尝试在django管理控制台中查看名称不是英文的对象时出现此错误。
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
我的.py文件标题是
-*- coding: utf-8 -*-
这是代码
class Owner(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50)
def __str__(self):
return self.name
我四处寻找有关该主题的其他主题,并找到了以下解决方案:
我已经尝试将py文件中的编码更改为unicode,我尝试将__str__
写为unicode(self.name)
和u'%s',self.name
。我还尝试添加具有相同组合的__unicode__
成员。没工作。我也尝试过再次同步数据库并弹出服务器。什么都没有。
奇怪的是名字显示在菜单中,但是当我试图编辑对象时,我收到了错误。
我将不胜感激。
Python 2.7,Django 1.5.2,Ubuntu 12,Postgres。
完全追溯异常
Internal Server Error: /admin/calc/owner/3/
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 140, in get_response
response = response.render()
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py", line 82, in rendered_content
content = template.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 140, in render
return self._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 830, in render
bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node
return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 124, in render
return compiled_parent._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 830, in render
bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node
return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 124, in render
return compiled_parent._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 830, in render
bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node
return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 284, in render
return nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 830, in render
bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node
return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 63, in render
result = block.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 830, in render
bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node
return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 284, in render
return nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 830, in render
bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 74, in render_node
return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 84, in render
output = self.filter_expression.resolve(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 606, in resolve
new_obj = func(obj, *arg_vals)
File "/usr/local/lib/python2.7/dist-packages/django/template/defaultfilters.py", line 41, in _dec
args[0] = force_text(args[0])
File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py", line 107, in force_text
s = six.text_type(bytes(s), encoding, errors)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
答案 0 :(得分:12)
更改您的代码:
class Owner(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name