get unicode error when print in html flask -
getting unnicode error when print in html flask. here code.
{% if(backpaths) %} {% n in backpaths:%} {% print '%s'%n %} </br> {% endfor %} {% endif %} i tried use n.decode('utf-8') didn't work , got same error
backpaths set to:
['1\xe6\x9c\x89 --(hyper)--> quantifier={indefinite|\xe4\xb8\x8d\xe5\xae\x9a\xe6\x8c\x87} --(hypo)--> \xe6\x9c\x89 ', '2\xe6\x9c\x89 --(hyper)--> exist|\xe5\xad\x98\xe5\x9c\xa8 --(hypo)--> \xe6\x9c\x89 '] here traceback
(most recent call last): file "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__ return self.wsgi_app(environ, start_response) file "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e)) file "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception reraise(exc_type, exc_value, tb) file "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app response = self.full_dispatch_request() file "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request rv = self.handle_user_exception(e) file "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception reraise(exc_type, exc_value, tb) file "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request rv = self.dispatch_request() file "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) file "/home/ganchimeg/flaskapp/flaskapp/__init__.py", line 125, in homepage return render_template('index.html', backpaths=successpaths) file "/usr/local/lib/python2.7/dist-packages/flask/templating.py", line 128, in render_template context, ctx.app) file "/usr/local/lib/python2.7/dist-packages/flask/templating.py", line 110, in _render rv = template.render(context) file "/usr/local/lib/python2.7/dist-packages/jinja2/environment.py", line 969, in render return self.environment.handle_exception(exc_info, true) file "/usr/local/lib/python2.7/dist-packages/jinja2/environment.py", line 742, in handle_exception reraise(exc_type, exc_value, tb) file "/home/ganchimeg/flaskapp/flaskapp/templates/index.html", line 31, in top-level template code {{ n }} unicodedecodeerror: 'ascii' codec can't decode byte 0xe6 in position 16: ordinal not in range(128)
you don't have unicode strings, have byte strings. python tries implicitly decode using standard ascii codec. explicitly decode them:
{% if(backpaths) %} {% n in backpaths:%} {{ n.decode('utf8') }} </br> {% endfor %} {% endif %} it'll better if passed in backpaths template ready-decoded.
Comments
Post a Comment