V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Etian
V2EX  ›  问与答

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

  •  1
     
  •   Etian · 2015-01-01 23:22:52 +08:00 · 4229 次点击
    这是一个创建于 3428 天前的主题,其中的信息可能已经有所发展或是发生改变。
    文件结构
    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)


    请多指教!
    2 条回复    2015-01-02 12:20:34 +08:00
    Etian
        1
    Etian  
    OP
       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
        2
    Etian  
    OP
       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
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3058 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 00:51 · PVG 08:51 · LAX 17:51 · JFK 20:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.