0

I'm trying to link to a static css file in Flask. However, the static route raises UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 8: ordinal not in range(128) when trying to send the file. Why am I getting this error and how do I fix it?

<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='content/alpin.css') }}"/>
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\flask-0.10.1-py2.7.egg\flask\app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python27\lib\site-packages\flask-0.10.1-py2.7.egg\flask\app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "C:\Python27\lib\site-packages\flask-0.10.1-py2.7.egg\flask\app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python27\lib\site-packages\flask-0.10.1-py2.7.egg\flask\app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python27\lib\site-packages\flask-0.10.1-py2.7.egg\flask\app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python27\lib\site-packages\flask-0.10.1-py2.7.egg\flask\app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python27\lib\site-packages\flask-0.10.1-py2.7.egg\flask\app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python27\lib\site-packages\flask-0.10.1-py2.7.egg\flask\app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Python27\lib\site-packages\flask-0.10.1-py2.7.egg\flask\helpers.py", line 822, in send_static_file
    cache_timeout=cache_timeout)
  File "C:\Python27\lib\site-packages\flask-0.10.1-py2.7.egg\flask\helpers.py", line 612, in send_from_directory
    filename = safe_join(directory, filename)
  File "C:\Python27\lib\site-packages\flask-0.10.1-py2.7.egg\flask\helpers.py", line 582, in safe_join
    return os.path.join(directory, filename)
  File "C:\Python27\lib\ntpath.py", line 85, in join
    result_path = result_path + p_path
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 8: ordinal not in range(128)
davidism
  • 121,510
  • 29
  • 395
  • 339

1 Answers1

3

As the Python traceback tells you, there is a UnicodeDecodeError.

I would expect it comes from your call to render_template Flask function.
The render_template function uses Jinja which expects Unicode strings.

By default, a string in Python 2.x is a byte string.
To change it to Unicode use:

>>> byte_string = 'I have a non-ASCII character: €'
>>> type(byte_string)
<type 'str'>
>>> unicode_string = byte_string.decode('utf-8')
>>> type(unicode_string)
<type 'unicode'>

Concretely what you can do is:

  • convert the string you pass to render_template to Unicode
  • enable Unicode string by default by using

    import sys
    reload(sys)
    sys.setdefaultencoding("utf-8")
    

    This is not the safest option though (see here)

If it is for a new project, you might use Python 3 directly which uses Unicode by default.

Community
  • 1
  • 1
filaton
  • 2,257
  • 17
  • 27