7

尝试在 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__asunicode(self.name)和 as 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)
4

1 回答 1

12

像这样更改您的代码:

class Owner(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50)
    def __unicode__(self):
        return self.name

请参阅此链接:管理页面上的 django unicode 错误

于 2013-11-29T09:25:20.597 回答