在 django admin 中访问数据库时出现 UnicodeEncodeError
UnicodeEncodeError while accessing db in django admin
尝试通过管理员访问数据库时出现此错误
UnicodeEncodeError at /admin/nota_app/demographic/
Exception Type: UnicodeEncodeError
Exception Value: 'ascii' codec can't encode character u'\x8e' in position 0: ordinal not in range(128)
这是我的模型:
@python_2_unicode_compatible
class Demographic(models.Model):
status = models.CharField(max_length = 100)
region = models.CharField(max_length = 50)
...
我以这种方式保存对象:
new_demographic = Demographic(
status = smart_unicode(my_dict[i]['status']),
region = smart_unicode(my_dict[i]['network']),
...
)
new_demographic.save()
我也尝试过使用unicode()
和encode('utf-8')
方法,但遗憾的是,它们也没有效果。谁能帮我解决这个问题?
这是完整的回溯:
UnicodeEncodeError at /admin/nota_app/demographic/
'ascii' codec can't encode character u'\x8e' in position 0: ordinal not in range(128)
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/nota_app/demographic/
Django Version: 1.7.9
Exception Type: UnicodeEncodeError
Exception Value:
'ascii' codec can't encode character u'\x8e' in position 0: ordinal not in range(128)
Exception Location: C:\Users\KESHAV\Desktop\StackQueue\nota\nota_app\models.py in __str__, line 87
Python Executable: C:\Users\KESHAV\Desktop\StackQueue\Scripts\python.exe
Python Version: 2.7.10
Python Path:
['C:\Users\KESHAV\Desktop\StackQueue\nota',
'C:\Windows\system32\python27.zip',
'C:\Users\KESHAV\Desktop\StackQueue\DLLs',
'C:\Users\KESHAV\Desktop\StackQueue\lib',
'C:\Users\KESHAV\Desktop\StackQueue\lib\plat-win',
'C:\Users\KESHAV\Desktop\StackQueue\lib\lib-tk',
'C:\Users\KESHAV\Desktop\StackQueue\Scripts',
'C:\Python27\Lib',
'C:\Python27\DLLs',
'C:\Python27\Lib\lib-tk',
'C:\Users\KESHAV\Desktop\StackQueue',
'C:\Users\KESHAV\Desktop\StackQueue\lib\site-packages']
Server time: Wed, 6 Jan 2016 16:28:48 +0000
如果您使用 @python_2_unicode_compatible
,那么您的 __str__
方法应该 return 一个 unicode 字符串。
你有 str(self.region+", "+self.country)
,它试图在 Python 中将 unicode 字符串转换为字节字符串 2. 要解决此问题,请将方法更改为:
def __str__(self):
return self.region + u"," + self.country
尝试通过管理员访问数据库时出现此错误
UnicodeEncodeError at /admin/nota_app/demographic/
Exception Type: UnicodeEncodeError
Exception Value: 'ascii' codec can't encode character u'\x8e' in position 0: ordinal not in range(128)
这是我的模型:
@python_2_unicode_compatible
class Demographic(models.Model):
status = models.CharField(max_length = 100)
region = models.CharField(max_length = 50)
...
我以这种方式保存对象:
new_demographic = Demographic(
status = smart_unicode(my_dict[i]['status']),
region = smart_unicode(my_dict[i]['network']),
...
)
new_demographic.save()
我也尝试过使用unicode()
和encode('utf-8')
方法,但遗憾的是,它们也没有效果。谁能帮我解决这个问题?
这是完整的回溯:
UnicodeEncodeError at /admin/nota_app/demographic/
'ascii' codec can't encode character u'\x8e' in position 0: ordinal not in range(128)
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/nota_app/demographic/
Django Version: 1.7.9
Exception Type: UnicodeEncodeError
Exception Value:
'ascii' codec can't encode character u'\x8e' in position 0: ordinal not in range(128)
Exception Location: C:\Users\KESHAV\Desktop\StackQueue\nota\nota_app\models.py in __str__, line 87
Python Executable: C:\Users\KESHAV\Desktop\StackQueue\Scripts\python.exe
Python Version: 2.7.10
Python Path:
['C:\Users\KESHAV\Desktop\StackQueue\nota',
'C:\Windows\system32\python27.zip',
'C:\Users\KESHAV\Desktop\StackQueue\DLLs',
'C:\Users\KESHAV\Desktop\StackQueue\lib',
'C:\Users\KESHAV\Desktop\StackQueue\lib\plat-win',
'C:\Users\KESHAV\Desktop\StackQueue\lib\lib-tk',
'C:\Users\KESHAV\Desktop\StackQueue\Scripts',
'C:\Python27\Lib',
'C:\Python27\DLLs',
'C:\Python27\Lib\lib-tk',
'C:\Users\KESHAV\Desktop\StackQueue',
'C:\Users\KESHAV\Desktop\StackQueue\lib\site-packages']
Server time: Wed, 6 Jan 2016 16:28:48 +0000
如果您使用 @python_2_unicode_compatible
,那么您的 __str__
方法应该 return 一个 unicode 字符串。
你有 str(self.region+", "+self.country)
,它试图在 Python 中将 unicode 字符串转换为字节字符串 2. 要解决此问题,请将方法更改为:
def __str__(self):
return self.region + u"," + self.country