@Mnemon, hats from you to solve my problem. I would raise you up, but I am not allowed to do this. You convinced me that if this is not the only webapp2 way without GAE, it is at least the way that will work.
But I can also contribute to the fact that your solution can now be installed as "pip install webapp2_static", from pipi --- by the author, who seems to use his real name ... you, I'm sure. Other webapp2 documents that I found useful are available here .
I implement your code on a Linux desktop development server using a paste that you also used:
def main(): from paste import httpserver httpserver.serve(app, host='127.0.0.1', port='8080')
But with the code as you have above (which seems to be completely identical to the webapp2_static.py file), I don’t think that placing my css files in a folder called static in the root of the application works the way you said.
For example, I have / home / user / proj / public _html / app / app.py where the py file contains your code plus other "views" for my ultra-simple site. (I don’t know how the paste works, so maybe public_html is now there for reference, so I’m not embarrassed when I upload the material to the production server.)
So, if I placed the css style sheets in a folder named / static, then if I placed / static as a subdirectory of / app or from / public _html, I found that no location works; I should instead make it a subdirectory of / proj.
I did not expect this, but because of this I need to change the static default value in your app.configure.get (..., 'static') application to 'public_html / app / static'. Then it works with the / static folder inside / app.
Similarly, using pipi code with the './app/static/ parameter instead of a static default does not work; I found what I need. / public _html / app / static (or maybe it's just / public _html / app / static or even public_html / app / static ... I forgot ... one of those who worked).
I tested how the calculation of abs_path works, and reworked it in the code below, in which I used your approach in favor of something more than Djangoesque. For this, in my one app py file, I put on top:
STATIC_DIR = os.sep + 'tostatic' + os.path.abspath(os.path.dirname(__file__)) + os.sep + 'static'
Then on the page to which I want to add css, my home page in my case, I put it very readable:
<link href="{{STATIC_DIR}}/dist/css/bootstrap.min.css" rel="stylesheet" type="text/css">
For the "view" that generates my homepage, I (env is a jinja2 environment object that takes a template loader as an argument):
class Home(webapp2.RequestHandler): def get(self): template = env.get_template('index.html') template_values = {'STATIC_DIR': STATIC_DIR } self.response.write(template.render(template_values))
And finally, the URL routing is this:
app = webapp2.WSGIApplication( [ (r'/', Home), (r'/tostatic/(.+)', StaticView), ], debug=True)
Now it shows what a static file looks like:
class StaticView(webapp2.RequestHandler): def get(self, path): path = os.sep + path try: f = open(path, 'r') self.response.headers.add_header('Content-Type', mimetypes.guess_type(path)[0]) self.response.out.write(f.read()) f.close() except Exception, e: print 'Problem in StaticView:', e self.response.set_status(404)
Finally, the problem I came across with your approach is that I and others are close to noobs sending URLs from an outdated file system association. In your approach, “static” is both a subdirectory and a line between the slash at the beginning of the URL that tells the interpreter what kind (which is done by the webapp2.RequestHandler subcategory). You take / static from the rest of the URL, and then hardcode it later. And when the time comes to decide what to put in the href in the tag, the HTML encoder should remember this duplicity. Using the template approach {{STATIC_DIR}} makes clear what to do. And it's easy to override the location of static files - you only need to change the STATIC_DIR declaration.
I found that self.response.set_status (404) appears in Firebug, but not in Firefox. Obviously, with webapp2, you must provide and maintain your own HTTP status code pages.