我正在开发一个新的Django站点,在迁移到一堆数据之后,我已经开始遇到一个非常令人沮丧的DjangoUnicodeDecodeError。问题中的坏角色是\xe8 8(e-坟墓)。
有两个棘手的问题:
这只发生在生产服务器上,运行Apache前端fcgi进程(在Django dev服务器上运行相同数据库的相同代码没有问题)。
所讨论的堆栈跟踪完全在Django代码中。当检索要显示的项时,它也发生在管理站点(其他地方),尽管包含坏字符的字段实际上从未呈现过。
我甚至不完全确定从哪里开始调试,除非尝试手动删除违规字符。我的猜测是,这是一个配置问题,因为它是特定于环境的,但我也不知道从哪里开始。
编辑:正如Daniel指出的,错误几乎肯定在unicode方法中--或者更准确地说,是它调用的另一种方法。请注意,违规字符位于代码中根本没有引用的字段中。我假设异常是在从db结果构建对象的方法中引发的--如果查询集从未被求值(例如,如果不是self.enabled),则没有错误。下面是代码:
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为了好玩,下面是堆栈跟踪的尾部:
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'>)发布于 2010-11-10 01:29:32
这可能是由于连接到Server的FreeTDS层造成的。虽然FreeTDS为自动转换编码提供了一些支持,但我的设置要么配置错误,要么无法正常工作。
现在,我没有参加这场战斗,而是迁移到了MySQL。
发布于 2010-11-06 18:51:59
这里的问题是在unicode中使用以下行:
label += " ({0})".format(blocking_msg)不幸的是,在python2.x中,这是试图将blocking_msg格式化为ascii字符串。你想输入的是:
label += u" ({0})".format(blocking_msg)https://stackoverflow.com/questions/4114419
复制相似问题