V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Jelly
V2EX  ›  Flask

Flask 请教一个关于 url_for('static')的问题

  •  
  •   Jelly · 2015-01-05 17:39:28 +08:00 · 11405 次点击
    这是一个创建于 3392 天前的主题,其中的信息可能已经有所发展或是发生改变。

    是这样的。。。
    这是我在base/post.html文件中写的:
    <link href="{{ url_for('static',filename='css/bootstrap.min.css') }}" rel="stylesheet">
    这是注册的蓝图:
    mod = Blueprint("base",name)
    @mod.route("/ogin")
    def login():
    return render_template("base/post.html")
    这是请求:
    http://127.0.0.1/ogin
    这是返回的资源:
    <link href="/ogin/static/css/style.css" rel="stylesheet">
    问题是:
    为什么返回的资源会有ogin前缀?我用nginx跑的,配置如下:
    location / {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME "";
    fastcgi_param PATH_INFO $fastcgi_script_name;
    fastcgi_pass unix:/tmp/o.sock;
    }

    location ~ ^/(static/){
        root /root_path/app;
        access_log off;
        expires max;
      }
    
    5 条回复    2015-01-07 04:52:52 +08:00
    wog
        1
    wog  
       2015-01-05 18:16:17 +08:00
    这样注册
    mod = Blueprint('base',name,
    static_folder="../static", template_folder="../templates")
    Jelly
        2
    Jelly  
    OP
       2015-01-05 18:26:17 +08:00
    @wog 还是不行,奇怪的是,我不用nginx,直接app.run()就会返回正确的资源
    repus911
        3
    repus911  
       2015-01-05 18:36:12 +08:00
    相对路径的问题?
    虽然我们也用falsk 不过static的用法不一样...
    Jelly
        4
    Jelly  
    OP
       2015-01-05 18:54:01 +08:00
    @repus911 经过了nginx后,url_for('static')的返回值就会加上请求的前缀,如果不用nginx+fcgi直接用flask跑,url_for('static')就会返回正确的资源,应该不是路径问题……
    yegle
        5
    yegle  
       2015-01-07 04:52:52 +08:00
    用了Blueprint,本来就会把所有URL加上前缀,你先检查一下是否需要用蓝图。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2738 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 04:08 · PVG 12:08 · LAX 21:08 · JFK 00:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.