Python Flask 从根静态文件夹而不是蓝图的静态文件夹服务静态文件
我有一个蓝图定义如下:
auth_login = Blueprint('auth_login', __name__, template_folder='templates', static_folder='static', static_url_path='/static')
我把它注册成这样:
app.register_blueprint(auth_login, url_prefix='/user')
我有一个路由:
@auth_login.route('/<username>/')
def profile(username):
return render_template('profile/user.html',
username = username)
按照这个设置,/user/ 这个地址无法加载静态文件,因为我收到了这样的错误:
"GET /user//static/css/lib/bootstrap.min.css HTTP/1.1" 404 -
"GET /user//static/css/lib/font-awesome/css/font-awesome.min.css HTTP/1.1" 404 -
我真正想要的是查看根应用的静态文件夹,而不是蓝图里的,所以我希望请求变成:
"GET /static/css/lib/bootstrap.min.css HTTP/1.1" 200 -
有没有办法通过 static_folder 或 static_url_path 来配置,让这个路由在根目录下查找静态文件,而不是相对于蓝图的位置?
1 个回答
5
默认情况下,Flask会把 /static
这个网址设置为从 {application_root}/static
这个文件夹里提供文件。你现在的代码是 覆盖 了这个默认设置。(虽然网址还是 /static
,但文件夹变成了 {application_root}/path/to/blueprint/static
,听起来这并不是你想要的结果。)
只需 去掉 你在 Blueprint
初始化时设置的 static_folder
和 static_url
参数,这样网址就应该能正常工作了(前提是你在 {application_root}/static
文件夹里放了合适的CSS文件)。