سؤال

I'm using bottle with gevent for my python webdev experiments. My problem is that I can't serve static files, e.g. use external css in my templates. My folders structure is: /static/css/style.css

My code:

index.py

# -*- coding: UTF-8 -*-
from gevent import monkey; monkey.patch_all() #patching default Python threads
from bottle import mount, run, debug #initializing bottle
from routes import root #importing site routes
debug( True )
run( app = root , host = '0.0.0.0' , port = 80 , server = 'gevent' )

routes.py

# -*- coding: UTF-8 -*-
from bottle import *
root = Bottle()

@root.get('/static/<path:path>')
def serve_files( path ):
    return static_file( path , root = '/static/' )

Here is my traceback from terminal:

xxx.xxx.xxx.xxx - - [2011-12-22 09:36:44] "GET /static/css/style.css HTTP/1.1" 500 161 0.002867
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/gevent-0.13.6-py2.7-linux-i686.egg/gevent/pywsgi.py", line 438, in handle_one_response
    self.run_application()
  File "/usr/local/lib/python2.7/dist-packages/gevent-0.13.6-py2.7-linux-i686.egg/gevent/pywsgi.py", line 424, in run_application
    self.result = self.application(self.environ, self.start_response)
  File "/usr/local/lib/python2.7/dist-packages/bottle-0.10.4-py2.7.egg/bottle.py", line 849, in __call__
    return self.wsgi(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/bottle-0.10.4-py2.7.egg/bottle.py", line 841, in wsgi
    % (html_escape(repr(_e())), html_escape(format_exc(10)))
NameError: global name '_e' is not defined

Please, help.

UPDATE:

I've downloaded unstable version of Bottle (version 0.11) and import it into my script. Now there is no 500 errors and tracebacks, but style.css gives me 404.

[2011-12-22 12:42:59] "GET /static/css/style.css HTTP/1.1" 404 122 0.000591
هل كانت مفيدة؟

المحلول

Your 404 is because probably your root path to static files is wrong.

root='/static/' is ok only if you have a static folder in your root file system. Probably it is not what you really have. If you have a project folder and inside this folder you have a static folder, use root='./static/' and it will work fine.

نصائح أخرى

I know this has been answered, but if you want something more production oriented, whitenoise is awesome.

from app import appRoute
from client import clientRoute
from main import mainRoute
from api import apiRoute
from beaker.middleware import SessionMiddleware
from whitenoise import WhiteNoise
if whitecompress:
    static_compress(whitecompress) # auto mins and gzips all js and css files
botapp = bottle.app()
for nftyRoute in (mainRoute, appRoute, clientRoute, apiRoute):          
    botapp.merge(nftyRoute)
botapp = SessionMiddleware(botapp, beaker_opts)
botapp = WhiteNoise(botapp)
botapp.add_files(staticfolder, prefix='static/')
botapp.add_files('{}/common/img/favicon.ico'.format(staticfolder), prefix='favicon.ico')
print('WhiteNoise Enabled')
WSGIServer(("0.0.0.0", int(port)), botapp, handler_class=WebSocketHandler).serve_forever()

This allows you to post your static files, and it will also handle delivering gzipped files for you automatically. I find this solution a tad more robust than the option built into bottle.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top