【发布时间】:2018-05-13 20:09:08
【问题描述】:
我正在尝试使用以下命令从模板 (index.html) 文件中引用静态文件。
{{ url_for('static', filename='test.css') }}
但是,我收到以下 500 错误:
127.0.0.1 - - [13/May/2018 21:34:49] "GET / HTTP/1.1" 200 -
[2018-05-13 21:34:49,603] ERROR in app: Exception on /static/test.css [GET]
Traceback (most recent call last):
File "/Users/username/folder/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/Users/username/folder/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/username/folder/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/username/folder/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/username/folder/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/username/folder/flask/helpers.py", line 976, in send_static_file
cache_timeout=cache_timeout)
File "/Users/username/folder/flask/helpers.py", line 713, in send_from_directory
return send_file(filename, **options)
File "/Users/username/folder/flask/helpers.py", line 628, in send_file
complete_length=fsize)
File "/Users/username/anaconda/lib/python2.7/site-packages/werkzeug/wrappers.py", line 1604, in make_conditional
accept_ranges = _clean_accept_ranges(accept_ranges)
File "/Users/username/anaconda/lib/python2.7/site-packages/werkzeug/wrappers.py", line 96, in _clean_accept_ranges
raise ValueError("Invalid accept_ranges value")
ValueError: Invalid accept_ranges value
127.0.0.1 - - [13/May/2018 21:34:49] "GET /static/test.css HTTP/1.1" 500 -
奇怪的是,Flask 会找到并发送模板文件,但它会导致静态文件出错。我怀疑 Flask 可以访问 CSS 文件,但拒绝发送它。我的怀疑是基于当我尝试移动或重命名 CSS 文件时收到 404 错误。这是我的文件夹结构:
|-- web
|-- static
|-- test.css
|-- templates
|-- index.html
更新 #1 这是 HTML 头部的内部:
<head>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='test.css') }}">
<title>Title</title>
</head>
【问题讨论】:
-
请包含模板代码。甚至只是
<head></head>里面的那些。 -
添加
app.py也会有帮助。 -
另外,请包含您在访问
/时从模板中获得的 HTML。同样,即使只是<head></head>中的那些。 -
如果您还没有这样做,您可能还想使用
app.run(debug=True)。 -
代码正确。我发现了问题。感谢您的回复!
标签: python flask static http-status-code-500