简体   繁体   中英

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

I'm trying to build a back-end interface for my application with flask-admin. When I try to access the form to create a new entry I get:

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

Going through the stack trace, the problem is that some items in my table contain non-ascii characters. How can I solve this issue? Thanks!

您应该使用unicode字符串,就像这样

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 sys
reload(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.

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