首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django字符编码

Django字符编码
EN

Stack Overflow用户
提问于 2010-11-06 18:12:16
回答 2查看 1.7K关注 0票数 1

我正在开发一个新的Django站点,在迁移到一堆数据之后,我已经开始遇到一个非常令人沮丧的DjangoUnicodeDecodeError。问题中的坏角色是\xe8 8(e-坟墓)。

有两个棘手的问题:

这只发生在生产服务器上,运行Apache前端fcgi进程(在Django dev服务器上运行相同数据库的相同代码没有问题)。

所讨论的堆栈跟踪完全在Django代码中。当检索要显示的项时,它也发生在管理站点(其他地方),尽管包含坏字符的字段实际上从未呈现过。

我甚至不完全确定从哪里开始调试,除非尝试手动删除违规字符。我的猜测是,这是一个配置问题,因为它是特定于环境的,但我也不知道从哪里开始。

编辑:正如Daniel指出的,错误几乎肯定在unicode方法中--或者更准确地说,是它调用的另一种方法。请注意,违规字符位于代码中根本没有引用的字段中。我假设异常是在从db结果构建对象的方法中引发的--如果查询集从未被求值(例如,如果不是self.enabled),则没有错误。下面是代码:

代码语言:javascript
复制
def get_blocking_events(self):
    return Event.objects.filter(<get a set of events>)

def get_blocking_reason(self):
    blockers = self.get_blocking_events()
    label = u''
    if not self.enabled:
        label = u'Sponsor disabled'
    elif len(blockers) > 0:
        label = u'Pending follow-up: "{0}" ({1})'.format(blockers[0],blockers[0].creator.email)
        if len(blockers) > 1:
            label += u" and {0} other event".format(len(blockers)-1)
        if len(blockers) > 2:
            label += u"s"
    return label

def __unicode__(self):
    label = self.name
    blocking_msg = self.get_blocking_reason()
    if len(blocking_msg):
        label += u" ({0})".format(blocking_msg)
    return label

为了好玩,下面是堆栈跟踪的尾部:

代码语言:javascript
复制
File "/opt/opt.LOCAL/Django-1.2.1/django/template/__init__.py", line 954, in render
   dict = func(*args)

 File "/opt/opt.LOCAL/Django-1.2.1/django/contrib/admin/templatetags/admin_list.py", line 209, in result_list
   'results': list(results(cl))}

 File "/opt/opt.LOCAL/Django-1.2.1/django/contrib/admin/templatetags/admin_list.py", line 201, in results
   yield list(items_for_result(cl, res, None))

 File "/opt/opt.LOCAL/Django-1.2.1/django/contrib/admin/templatetags/admin_list.py", line 138, in items_for_result
   f, attr, value = lookup_field(field_name, result, cl.model_admin)

 File "/opt/opt.LOCAL/Django-1.2.1/django/contrib/admin/util.py", line 270, in lookup_field
   value = attr()

 File "/opt/opt.LOCAL/Django-1.2.1/django/db/models/base.py", line 352, in __str__
   return force_unicode(self).encode('utf-8')

 File "/opt/opt.LOCAL/Django-1.2.1/django/utils/encoding.py", line 88, in force_unicode
   raise DjangoUnicodeDecodeError(s, *e.args)

DjangoUnicodeDecodeError: 'utf8' codec can't decode bytes in position 956-958: invalid data. You passed in <Sponsor: [Bad Unicode data]> (<class 'SJP.alcohol.models.Sponsor'>)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-10 01:29:32

这可能是由于连接到Server的FreeTDS层造成的。虽然FreeTDS为自动转换编码提供了一些支持,但我的设置要么配置错误,要么无法正常工作。

现在,我没有参加这场战斗,而是迁移到了MySQL。

票数 1
EN

Stack Overflow用户

发布于 2010-11-06 18:51:59

这里的问题是在unicode中使用以下行:

代码语言:javascript
复制
label += " ({0})".format(blocking_msg)

不幸的是,在python2.x中,这是试图将blocking_msg格式化为ascii字符串。你想输入的是:

代码语言:javascript
复制
label += u" ({0})".format(blocking_msg)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4114419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档