如何在Django中解决UnicodeDecodeError?


问题内容

我在Django中收到此错误:

 UnicodeDecodeError at /category/list/

 'utf8' codec can't decode byte 0xf5 in position 7: invalid start byte

 Request Method:    GET
 Request URL: ...
 Django Version:    1.3.1
 Exception Type:    UnicodeDecodeError
 Exception Value:

 'utf8' codec can't decode byte 0xf5 in position 7: invalid start byte

 Exception Location:                       /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py in iterencode, line 264
 ...

我应该将土耳其语字符保存在数据库中。如何解决此错误?


问题答案:

下面的代码解决了我的问题。谢谢。

if isinstance(encObject, unicode):
                           myStr = encObject.encode('utf-8')