2012-11-24 70 views
1

嘗試使用Bottle加載靜態文件時總是得到404。我提到了堆棧溢出問題和Google羣組聊天,但沒有什麼能幫助我糾正我的問題。請幫我..花很多就在這個時候......無法加載靜態文件CSS和JS通過pyBottle?

Tuts 
    main.py 
    static/ 
    bootstarp.css 
    views/ 
    index.tpl 

main.py代碼

import bottle 
from bottle import Bottle 
from os.path import basename,abspath, dirname, join 

app = bottle.default_app() 

appPath = dirname(abspath('')) 
print appPath 

@bottle.route('/') 
def index(): 
    return bottle.template('index', dict(title=appPath,get_url=app.get_url)) 

@bottle.route('/static/:filename#.*#', name='css') 
def server_static(filename): 
    return bottle.static_file(filename, root=join(appPath,'static')) 

bottle.debug(True) 
bottle.run(host='localhost', port=8082,reloader=True) 


**Template:** 

<link href="{{ get_url('css', filename='bootstrap.css') }}" rel="stylesheet" media="screen"> 

回答

0

添加

from bottle import static_file 

在頂部。那就用static_file(..., ...)就好了:

@bottle.route('/static/:filename#.*#', name='css') 
def server_static(filename): 
    return static_file(filename, root=join(appPath,'static'))