Search code examples
pythonflaskstatichttp-status-code-500

Flask static files return 500 error


I am trying to reference a static file with the following command, from within the template (index.html) file.

{{ url_for('static', filename='test.css') }}

However, I receive the following 500 error:

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 -

It is strange that Flask will find and send the template file, but it causes an error for the static file. I suspect that Flask can access the CSS-file, but refuses to send it. My suspicion is based on that I receive a 404 error when I try to move or rename the CSS-file. This is my folder structure:

|-- web
    |-- static
        |-- test.css
    |-- templates
        |-- index.html

Update #1 Here is the insides of the HTML head:

<head>
  <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='test.css') }}">
  <title>Title</title>
</head>

Solution

  • I think I have identified the error. I am trying to make the project portable, so it can be used by users that cannot install Flask. Thus, I include and load "flask" from a directory within my project.

    After renaming the directory to "flask_2" and running the program again, it runs completely fine on flask that I installed via pip.