【发布时间】:2017-01-25 13:55:19
【问题描述】:
我的 Flask 应用程序上有一个蓝图 home,前缀为 /。蓝图有一个静态文件夹,并使用 static_folder 参数进行配置。但是,链接到蓝图的静态文件会返回 404 错误,即使该文件存在并且 url 看起来正确。为什么蓝图不提供静态文件?
myproject/
run.py
myapp/
__init__.py
home/
__init__.py
templates/
index.html
static/
css/
style.css
myapp/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;
}
【问题讨论】:
-
也许您应该将您的蓝图移动到 init.py 文件中?
-
没有错误,只是一个白页。 body标签没有任何css样式,看不到背景。
-
这是我上一个问题对我项目的更多辩护:stackoverflow.com/questions/41828711/…