我试图链接到一个静态的css文件在瓶。但是,静态路由在试图发送文件时会引发UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 8: ordinal not in range(128)。为什么我要得到这个错误,以及如何修复它?
<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)发布于 2016-04-12 18:33:53
正如Python告诉您的那样,有一个UnicodeDecodeError。
我希望它来自您对render_template烧瓶函数的调用。
render_template函数使用Jinja,它需要Unicode字符串。
默认情况下,Python2.x中的字符串是字节字符串。
若要将其更改为Unicode,请使用:
>>> 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'>具体而言,你能做的是:
render_template的字符串转换为Unicode如果是针对新项目,则可以直接使用Python 3,默认情况下使用Unicode。
https://stackoverflow.com/questions/36580747
复制相似问题