无法通过pyBottle加载静态文件CSS和JS?

时间:2012-11-24 06:56:15

标签: python bottle

尝试使用Bottle加载静态文件时始终获取404。我已经提到了堆栈溢出问题和谷歌群聊,但没有任何东西可以帮助我纠正我的问题。请帮帮我..花了很多时间在这......

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">

1 个答案:

答案 0 :(得分: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'))