python - Static files in a Bottle application cannot be found (404) -
i've reviewed questions here this, reviewed bottle tutorial, reviewed bottle google group discussions, , afaik, i'm doing right. somehow, though, can't css file load properly. i'm getting 404 on static file, http://localhost:8888/todo/static/style.css not found, which, according directory structure below, should not case. i'm using version 0.11 (unstable) of bottle; there i'm missing, or bug in bottle?
my directory structure:
todo/ todo.py static/ style.css my todo.py:
import sqlite3 bottle import bottle, route, run, debug, template, request, validate, static_file, error, simpletemplate # needed when run bottle on mod_wsgi bottle import default_app app = bottle() default_app.push(app) apppath = '/applications/mamp/htdocs/todo/' @app.route('/todo') def todo_list(): conn = sqlite3.connect(apppath + 'todo.db') c = conn.cursor() c.execute("select id, task todo status '1';") result = c.fetchall() c.close() output = template(apppath + 'make_table', rows=result, get_url=app.get_url) return output @route('/static/:filename#.*#', name='css') def server_static(filename): return static_file(filename, root='./static') my html:
%#template generate html table list of tuples (or list of lists, or tuple of tuples or ...) <head> <link href="{{ get_url('css', filename='style.css') }}" type="text/css" rel="stylesheet" /> </head> <p>the open items follows:</p> <table border="1"> %for row in rows: <tr style="margin:15px;"> %i = 0 %for col in row: %if == 0: <td>{{col}}</td> %else: <td>{{col}}</td> %end %i = + 1 %end <td><a href="/todo/edit/{{row[0]}}">edit</a></td> </tr> %end </table>
i don't quite understand deployment. /applications/mamp/htdocs/ path, along lack of app.run in code, suggest you're running under apache. production deployment? dev tasks supposed use bottle's built-in dev server, know. add single app.run() towards end of todo.py, , you're done.
now if using apache, root cause line: static_file(filename, root='./static'). mod_wsgi, not guaranteed working directory equal directory todo.py placed. in fact, never be.
you using absolute paths database , template, static files:
@route('/static/:filename#.*#', name='css') def server_static(filename): return static_file(filename, root=os.path.join(apppath, 'static')) next, don't understand app mounted. url http://localhost:8888/todo/static/style.css suggests mount point /todo, route todo_list handler again /todo. full path supposed http://localhost/todo/todo? app have / handler?
i'd suggest avoid hard-coding paths , concat'ing path fragments together. cleaner:
from os.path import join, dirname ... apppath = dirname(__file__) @app.route('/todo') def todo_list(): conn = sqlite3.connect(join(apppath, 'todo.db')) ...
Comments
Post a Comment