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

2015-01-05 17:39:28 +08:00
 Jelly

是这样的。。。
这是我在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;
  }
11422 次点击
所在节点    Flask
5 条回复
wog
2015-01-05 18:16:17 +08:00
这样注册
mod = Blueprint('base',name,
static_folder="../static", template_folder="../templates")
Jelly
2015-01-05 18:26:17 +08:00
@wog 还是不行,奇怪的是,我不用nginx,直接app.run()就会返回正确的资源
repus911
2015-01-05 18:36:12 +08:00
相对路径的问题?
虽然我们也用falsk 不过static的用法不一样...
Jelly
2015-01-05 18:54:01 +08:00
@repus911 经过了nginx后,url_for('static')的返回值就会加上请求的前缀,如果不用nginx+fcgi直接用flask跑,url_for('static')就会返回正确的资源,应该不是路径问题……
yegle
2015-01-07 04:52:52 +08:00
用了Blueprint,本来就会把所有URL加上前缀,你先检查一下是否需要用蓝图。

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

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

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

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

© 2021 V2EX