简体   繁体   中英

Flask: rendering unicode characters in template

In a Flask app I'm working on, I need to display Unicode text to users. When testing them out in the Python shell, my functions seem to work fine:

>>> sr = rym_scraper.get_artist_info('sigur ros')
>>> print sr.name # a string encoded using str.encode('utf8')
Sigur Rós

When I actually test it out in the app, I get this:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 3: ordinal not in range(128)

With the final exception being in the template it produces this stack trace:

Traceback (most recent call last):
  File "/Users/eric/Envs/whatshouldilistento/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/eric/Envs/whatshouldilistento/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Users/eric/Envs/whatshouldilistento/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/eric/Envs/whatshouldilistento/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/eric/Envs/whatshouldilistento/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/eric/Envs/whatshouldilistento/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/eric/projects/whatshouldilistento/app/app.py", line 29, in enter_band
    return band_info(form.name.data)
  File "/Users/eric/projects/whatshouldilistento/app/app.py", line 45, in band_info
    return render_template('band_info.html')
  File "/Users/eric/Envs/whatshouldilistento/lib/python2.7/site-packages/flask/templating.py", line 125, in render_template
    context, ctx.app)
  File "/Users/eric/Envs/whatshouldilistento/lib/python2.7/site-packages/flask/templating.py", line 107, in _render
    rv = template.render(context)
  File "/Users/eric/Envs/whatshouldilistento/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/Users/eric/Envs/whatshouldilistento/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/eric/projects/whatshouldilistento/app/templates/band_info.html", line 9, in top-level template code
    <div>{{ message }}</div>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 3: ordinal not in range(128)

How can I display the Unicode string in my rendered template?

add below to your start up file

import sys
if sys.version_info.major < 3:
    reload(sys)
sys.setdefaultencoding('utf8')

this works for me

Your message is non-ascii, you have to decode it and convert it to unicode .

your can convert it using.

{{ message.decode('utf-8') }}

My unicode strings were embedded in arrays and objects, and I passed them using json.dumps() to get rid of the unicode references.

https://stackoverflow.com/a/50612950/999943

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM