【问题标题】:UnicodeDecodeError in django top-level template codedjango顶级模板代码中的UnicodeDecodeError
【发布时间】:2014-09-16 05:03:32
【问题描述】:

我的视图中有代码返回要在文本框中显示的信息。我的名字在导致 UnicodeDecodeErrors 的字母上有 fadas(爱尔兰口音)。我的逻辑是这样的:

return {
    ...
    'wrap_up_form': WrapUpForm(data={u'message': settings.DEFAULT_WRAP_UP_MESSAGE.format(name=customer.given_name.encode('utf-8'))}),
}

我得到的回溯是这样的

ERROR    2014-07-24 14:48:26,540 exception_handlers.py:65] 'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128)
Traceback (most recent call last):
  File "/home/rony/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/home/rony/Documents/clone-attempt/personal-shopping/vendor/nacelle/core/dispatcher.py", line 24, in nacelle_dispatcher
    response = router.default_dispatcher(request, response)
  File "/home/rony/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/home/rony/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1065, in __call__
    return self.handler(request, *args, **kwargs)
  File "/home/rony/Documents/clone-attempt/personal-shopping/app/utils/decorators.py", line 43, in _arguments_wrapper
    return view_method(request, *args, **kwargs)
  File "/home/rony/Documents/clone-attempt/personal-shopping/app/utils/decorators.py", line 89, in _arguments_wrapper
    output = render_jinja2_template(template_name, context)
  File "/home/rony/Documents/clone-attempt/personal-shopping/vendor/nacelle/core/template/renderers.py", line 19, in render_jinja2_template
    return renderer.render_template(template_name, **context)
  File "/home/rony/google_appengine/lib/webapp2-2.5.2/webapp2_extras/jinja2.py", line 158, in render_template
    return self.environment.get_template(_filename).render(**context)
  File "/home/rony/google_appengine/lib/jinja2-2.6/jinja2/environment.py", line 894, in render
    return self.environment.handle_exception(exc_info, True)
  File "templates/cms/appointments_form.html", line 2, in top-level template code
    {% import 'cms/macros.html' as cms_macros %}
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128)

我需要在我的模板中添加某种编码吗?

【问题讨论】:

  • 如果你删除.encode('utf-8')会发生什么?
  • 我现在得到一个 UnicodeEncodeError: gist.github.com/anonymous/e06491e967638cb44e3e
  • 如果你使用decode而不是encode呢?
  • UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 1: ordinal not in range(128)
  • 尝试将文字更改为 Unicode 文字 (DEFAULT_WRAP_UP_MESSAGE = u"""Hey {name}, We ....""")。 (并从导致错误的行中删除 decode(..)

标签: python django python-2.7


【解决方案1】:

customer.given_name 似乎很可能是一个字节字符串,而不是 Unicode - 因此在对其调用 encode 时,Python 首先需要将其解码为 Unicode,然后才能重新编码为 UTF-8。

您应该完全放弃编码调用。

【讨论】:

【解决方案2】:

正如丹尼尔罗斯曼回答的那样,我也怀疑customer.given_name 是字节字符串;尝试encode 导致 Python 尝试对其进行解码。

另一个问题是DEFAULT_WRAP_UP_MESSAGE 是字节字符串文字。

str.format(unicode) 有同样的问题。


解决方案:

  • 删除.decode(..) 部分。
  • 创建一个 DEFAULT_WRAP_UP_MESSAGE unicode 对象而不是字节字符串。

【讨论】:

    猜你喜欢
    • 2011-05-13
    • 2014-10-16
    • 1970-01-01
    • 2022-11-13
    • 2017-10-24
    • 2019-09-13
    • 1970-01-01
    • 2015-03-18
    • 2014-01-15
    相关资源
    最近更新 更多