[英]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: 解:
.decode(..)
part. .decode(..)
部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.