我有一个蓝图,home,在我的Flask应用程序上有前缀/。蓝图有一个静态文件夹,并使用static_folder参数进行配置。然而,链接到蓝图的静态文件会返回404错误,即使该文件存在并且url看起来是正确的。为什么蓝图不支持静态文件?
myproject/
run.py
myapp/
__init__.py
home/
__init__.py
templates/
index.html
static/
css/
style.cssmyapp/init.py
from flask import Flask
application = Flask(__name__)
from myproject.home.controllers import home
application.register_blueprint(home, url_prefix='/')myapp/home/controllers.py
from flask import Blueprint, render_template
home = Blueprint('home', __name__, template_folder='templates', static_folder='static')
@home.route('/')
def index():
return render_template('index.html')myapp/home/templates/index.html
<head>
<link rel="stylesheet" href="{{url_for('home.static', filename='css/style.css')}}">
</head>
<body>
</body>myapp/home/static/css/style.css
body {
background-color: green;
}发布于 2017-01-25 23:05:09
您将与Flask静态文件夹和蓝图发生冲突。由于蓝图挂载在/上,因此它与应用程序共享相同的静态url,但应用程序的路由优先。更改蓝图的静态url,这样它就不会冲突。
home = Blueprint(
'home', __name__,
template_folder='templates',
static_folder='static',
static_url_path='/home-static'
)发布于 2017-01-26 16:00:22
最后,根据朋友的答案,我自己找到了正确的答案。唯一的更改应该如下所示:
myapp/init.py:
application = Flask(__name__, static_folder=None)myapp/home/Controlers.py:
home = Blueprint('home', __name__, template_folder='templates', static_folder='static', static_url_path='static')myapp/home/templates/index.html:
<link rel="stylesheet" href="{{url_for('home.static', filename='style.css')}}">https://stackoverflow.com/questions/41853436
复制相似问题