Python bottle 框架引用 bootstrap 里的静态文件,路径总是错误,请大大们不吝赐教!

2015-01-01 23:22:52 +08:00
 Etian
文件结构
Package: main
---static
--css
--bootstrap.min.css
--style.css
--js
--img
-- main.py
--main.tpl

run main.py后加载出main.tpl页面,在main.tpl页面需要引用css静态文件


具体代码如下:
from bottle import get, post, request, route, run, template, static_file
import os

@get('/main')
def index():
currentPath=os.path.dirname(os.path.realpath(__file__)) #获取当前路径
print currentPath
return template(currentPath+r'/main')

@route('/main/static/css/<bootstrap>')
def server_static(bootstrap):
return static_file(bootstrap, root='./main/static/css')

run(host='localhost', port=8081)


请多指教!
4243 次点击
所在节点    问与答
2 条回复
Etian
2015-01-02 00:22:23 +08:00
解决了:
@route('/static/css/<filename>')
def server_static(filename):
return static_file(filename, root='./static/css/')


然后在浏览器中输入 http://localhost:8081/static/css/bootstrap.min.css 就不再404了
Etian
2015-01-02 12:20:34 +08:00
看文档的时候以为这个filename要写成自己要引用的css文件名,其实就按照文档给的模式写,在具体要请求哪个文件的时候再加上具体的文件名就行了。
<link href="/static/css/bootstrap.min.css" rel="stylesheet">

参考文档 http://bottlepy.org/docs/dev/tutorial.html

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/158520

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX