问题描述
好像我的静态文件没有正确提供.这就是现在的样子:
Seems like my static files aren't being served properly. This is what it looks like right now:
myApp
__init__.py
static
img
js
bootstrap.min.js
etc.
这是我 __ init __.py
中的应用程序配置的样子:
This is what my app config in my __init__.py
looks like:
app = Flask(__name__, static_url_path="", static_folder="static")
这是错误:
127.0.0.1 - - [01/Dec/2014 13:12:01] "GET /static/js/bootstrap.min.js HTTP/1.1" 404 -
就url路由而言,没有问题,本地主机的localhost/home路由,本地主机的localhost/contact路由等.但是找不到静态文件:(我缺少什么吗?
As far as the url routing goes, no problems there, localhost/home routes to home, localhost/contact routes to contact, etc. But static files aren't being found :( Am I missing something?
注意:我是在Mac(纯粹是本地主机)上托管该主机
NOTE: I'm hosting this on my Mac, purely local host
这是我的 __ init __.py
主要方法:
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)
运行方式:
python __init.py__
推荐答案
似乎您正在硬编码到静态文件URL.您告诉Flask在没有URL前缀的情况下为它们提供服务器-默认值为/static
-但日志显示请求将发送至/static/js/bootstrap.min.js
.模板文件中可能包含以下内容.
It looks like you are hardcoding to static file URLs. You tell Flask to server them with no URL prefix -- the default would have been /static
-- but the log shows the request going to /static/js/bootstrap.min.js
. You probably have something like the following in a template file.
<script src="/static/js/bootstrap.min.js"></script>
相反,您应该使用 url_for
为您创建URL.将以上更改为
Instead, you should use url_for
to create the URL for you. Change the above to
<script src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>
这将导致
<script src="/js/bootstrap.min.js"></script>
这篇关于在Flask中设置静态文件夹路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!