简体   繁体   English

Django顶级模板代码中的UnicodeDecodeError

[英]UnicodeDecodeError in django top-level template code

I have code in my views that returns information to be displayed in a textbox. 我的视图中有代码,该代码返回要在文本框中显示的信息。 My name has fadas (Irish accent) over the letters which is causing UnicodeDecodeErrors. 我的名字的字母上有fadas(爱尔兰口音),这会导致UnicodeDecodeErrors。 The line in my logic is as follows: 我的逻辑行如下:

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

and the traceback I get is this 我得到的回溯是

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)

Do I need to add some sort of encoding to my templates? 我是否需要在模板中添加某种编码?

It seems likely that customer.given_name is a byte string, rather than Unicode - so in calling encode on it, Python first needs to decode it to Unicode before it can then re-encode to UTF-8. customer.given_name似乎是一个字节字符串,而不是Unicode-因此,在对其调用encode时,Python首先需要将其解码为Unicode,然后才能将其重新编码为UTF-8。

You should drop the encode call altogether. 您应该完全删除编码调用。

As Daniel Roseman answered, I also suspect that customer.given_name is byte string; 正如Daniel Roseman回答的那样,我也怀疑customer.given_name是字节字符串; trying to encode on it cause Python try to decode it. 尝试对其encode会导致Python尝试对其进行解码。

Another issue is that DEFAULT_WRAP_UP_MESSAGE is byte string literal. 另一个问题是DEFAULT_WRAP_UP_MESSAGE是字节字符串文字。

str.format(unicode) has same issue. str.format(unicode)有相同的问题。


Solution: 解:

  • remove .decode(..) part. 删除.decode(..)部分。
  • Make a DEFAULT_WRAP_UP_MESSAGE unicode object instead of byte string. 使一个DEFAULT_WRAP_UP_MESSAGE Unicode对象而不是字节字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django中有关Page的顶级错误 - Top-level about Page in Django error 文件 ”<template> &quot;,第 1 行,在顶级模板代码 jinja2.exceptions.UndefinedError: &#39;Check&#39; 未定义 - File "<template>", line 1, in top-level template code jinja2.exceptions.UndefinedError: 'Check' is undefined Django模板不存在不检查顶级.html吗? - Django Template Doesn't Exist doesn't check top-level .html? StreamField 中的顶级 StreamBlock 不在模板中呈现 - Top-level StreamBlock within StreamField doesn't render in template 从具有不同顶级文件夹名称的Django项目导入 - Import from a Django project with a different top-level folder name 导入错误:尝试在 Django 中的顶级包之外进行相对导入 - ImportError: attempted relative import beyond top-level package in django ImportError:尝试相对导入超出顶级 package django - ImportError: attempted relative import beyond top-level package django 如何从代码中自动在sphinx中创建顶级文档? - How to create top-level documentation in sphinx automatically from code? 如何访问python包中顶级文件中的代码 - How to access code in a top-level file in a python package 避免在单元测试中运行顶级模块代码 - Avoiding running top-level module code in unit test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM