Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django顶级模板代码中的UnicodeDecodeError_Python_Django_Python 2.7 - Fatal编程技术网

Python django顶级模板代码中的UnicodeDecodeError

Python django顶级模板代码中的UnicodeDecodeError,python,django,python-2.7,Python,Django,Python 2.7,我的视图中有代码,返回要在文本框中显示的信息。我的名字的字母上带有爱尔兰口音,这导致了UnicodeDecodeErrors。我的逻辑是这样的: return { ... 'wrap_up_form': WrapUpForm(data={u'message': settings.DEFAULT_WRAP_UP_MESSAGE.format(name=customer.given_name.encode('utf-8'))}), } 我得到的追踪结果是 ERROR 2014

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

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)

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

似乎是
客户。给定\u name
是一个字节字符串,而不是Unicode-因此在调用
encode
时,Python首先需要将其解码为Unicode,然后才能重新编码为UTF-8


您应该完全放弃encode调用。

正如Daniel Roseman回答的那样,我还怀疑
客户。给定的\u name
是字节字符串;尝试对其进行
编码
会导致Python尝试对其进行解码

另一个问题是,
DEFAULT\u WRAP\u-UP\u消息
是字节字符串文字

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


解决方案:

  • 删除
    。解码(..)
    部件
  • 使用默认的消息unicode对象而不是字节字符串

如果删除
.encode('utf-8')
,会发生什么情况?我现在得到一个Unicodeincoder错误:gist.github.com/anonymous/e06491e967638cb44e3e如果使用
解码
而不是
编码
Unicodeincoder:'ascii'编解码器无法对位置1中的字符u'\xf3'进行编码:序号不在范围内(128)
尝试将文本更改为unicode文本(
默认值\u WRAP\u MESSAGE=u“”“嘿{name},我们…”
)。(并从导致错误的行中删除
decode(..)
)我现在得到一个
unicodeincoder错误