我的文件结构:
my_app
/models
/views
/templates
/other_folders
/www (where flask resides)
/templates
some_files.html
/static
/css
style.css
/views
/models
my_app.py (run the app in console)
web_my_app.py (run flask app)
在 web_my_app.py 中,我的 static_folder 是这样设置的:
app = Flask('my_app',
template_folder='www/templates/',
static_folder='www/static'
)
# As a test, let's develop a basic page
@app.route('/')
def show_index():
return render_template('main.html', entry="hello")
main.html 看起来像这样:
{# Main page #}
{% extends "layout.html" %}
{% block body %}
<h2>Main Page</h2>
{{ entry }}
{% endblock %}
layout.html 具有以下样式表
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}">
问题:为什么我的静态页面上会出现 404 而模板上却没有?
该应用设法很好地访问模板文件,但不知道静态页面在哪里。
请您参考如下方法:
我想您会发现静态文件很好并且可以在 http://localhost:5000/www/static/... 访问。
默认情况下,Flask 的开发服务器使用您在 static_folder 参数中提供的相同 URL 前缀提供静态文件。如果您希望 URL 不同,您必须将 static_url_path 参数添加到 Flask 构造函数中。示例:
app = Flask('my_app',
template_folder='www/templates/',
static_folder='www/static',
static_url_path='/static'
)
现在您可以在 http://localhost:5000/static/...
