Flask-Admin: UnicodeDecodeError: 'ascii' codec can't decode byte Flask-Admin: UnicodeDecodeError: 'ascii' codec can't decode byte flask flask

Flask-Admin: UnicodeDecodeError: 'ascii' codec can't decode byte


You should use unicode string, which is like this

u"whaterver string"


Just had the same problem with some legacy code. This problem happens whenever you:

  1. Use Python 2, and
  2. Represent non-ASCII data via str (rather than unicode) objects, and
  3. Your Python system encoding is ascii (as is mostly the case).

The second issue may stem from having SQLAlchemy String columns where you shold have had Unicode, or from writing 'šömething' when you should have written `u'šömething'' - it may often be quite tricky to pinpoint the actual source of the problem.

It is, however, easy to solve it by hacking the third component of the equation (which is not recommended in general, though). Adding these lines somewhere in your code should fix things (by tucking the actual problem under the carpet):

import sysreload(sys)sys.setdefaultencoding('UTF8')


In general, this error is solved by forcing the string array to unicode with the unicode.encode() method .

From the Python Wiki page on the subject

>>> u"a".encode("utf-8")'a'>>> u"\u0411".encode("utf-8")'\xd0\x91'>>> "a".encode("utf-8")         # Unexpected argument type.'a'>>> "\xd0\x91".encode("utf-8")  # Unexpected argument type.Traceback (most recent call last):  File "<stdin>", line 1, in <module>UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in     range(128)

I suppose this would be best solved by modifying the jinja macro responsible for the field formatting to force the values to unicode.