[英]Python - 'ascii' codec can't encode character u'\xe9' in position 5: ordinal not in range(128)
I'm trying to run a quick Django application that pulls data from Google AdWords and exposes the names of accounts that are managed by an agency.我正在尝试运行一个快速的 Django 应用程序,该应用程序从 Google AdWords 中提取数据并公开由代理机构管理的帐户名称。 When doing so, I get the following error:
这样做时,我收到以下错误:
UnicodeEncodeError at /account-hierarchy/
'ascii' codec can't encode character u'\xe9' in position 5: ordinal not in range(128)
Here's the snippet:这是片段:
<table class="pretty-table">
<thead>
<tr>
<td>Customer ID</td>
<td>Client Name</td>
<td>Can Manage Clients</td>
<td>Account Currency</td>
</tr>
</thead>
{% for account in managed_accounts %}
<tr>
{% for field in account %}
<td>{{ field }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
where the call to {{ field }}
is the problematic line.其中对
{{ field }}
的调用是有问题的行。
I have already added我已经添加了
<meta http-equiv="content-type" content="text/html; charset=utf-8">
to the template I am rendering, but it still fails, so I believe the problem is not on the HTML template, but rather on the Python/Django engine.到我正在渲染的模板,但它仍然失败,所以我相信问题不在于 HTML 模板,而在于 Python/Django 引擎。
Any ideas how I can fix it?有什么想法可以解决吗?
Here's the View code that renders the template:这是呈现模板的视图代码:
def account_hierarchy(request):
manager_ids = settings.MANAGER_IDS
managed_accounts = []
for manager_id in manager_ids:
managed_accounts.extend(adwords_utils.getManagedAccounts(manager_id))
return render_to_response('simple-table.html', {"managed_accounts": managed_accounts})
UPDATED Question更新的问题
What's also curious is that if I remove this:同样奇怪的是,如果我删除它:
{% for field in account %}
<td>{{ field }}</td>
{% endfor %}
and I just print out the main array:我只是打印出主数组:
{{ managed_accounts }}
it works just fine.它工作得很好。 Not sure what's going on.
不知道发生了什么。
Curious fact #2: As I managed to output the full array, I checked for character 'é' and I didn't find it on the final output.奇怪的事实 #2:当我设法输出完整数组时,我检查了字符 'é',但在最终输出中没有找到它。 Not sure where it was coming from.
不知道它是从哪里来的。
The problem is likely that, in some place in your code, you accidentally defined a data structure to be a Python byte string, when you should have made it a Python Unicode string.问题很可能是,在您的代码中的某个地方,您不小心将数据结构定义为 Python 字节字符串,而您应该将其定义为 Python Unicode 字符串。 This leads Django and Python to convert from Django's Unicode string to your byte string, in a default way, using the ASCII codec.
这导致 Django 和 Python 以默认方式使用 ASCII 编解码器从 Django 的 Unicode 字符串转换为您的字节字符串。
The error message gives some clues:错误消息提供了一些线索:
account_hierarchy()
account_hierarchy()
时可能会发生错误character u'\\xe9' in position 5: ordinal not in range(128)
means that Python is trying to convert Unicode character 'é' (U+00E9) into an ASCII value from 0..127. character u'\\xe9' in position 5: ordinal not in range(128)
的短语character u'\\xe9' in position 5: ordinal not in range(128)
表示 Python 正在尝试将 Unicode 字符 'é' (U+00E9) 转换为 0..127 的 ASCII 值。 Look in your data for an 'é'.'ascii' codec
means the conversion is likely inadvertent.'ascii' codec
意味着转换可能是无意的。 If you intend to use UTF-8, you wouldn't have called the ASCII codec intentionally.encode
means conversion from Unicode to byte-oriented encoding. encode
一词意味着从 Unicode 转换为面向字节的编码。 You have read Python's Unicode HOWTO article a couple of times, haven't you? Function render_to_response() uses settings DEFAULT_CHARSET and DEFAULT_CONTENT_TYPE]( https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-DEFAULT_CONTENT_TYPE ).函数render_to_response()使用设置DEFAULT_CHARSET和 DEFAULT_CONTENT_TYPE]( https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-DEFAULT_CONTENT_TYPE )。 They should default to 'utf-8' and 'text/html', which should be appropriate for generating a UTF-8 encoded HTML page, but check.
它们应该默认为“utf-8”和“text/html”,这应该适合生成 UTF-8 编码的 HTML 页面,但请检查。
I like the suggestion that you check your models.py
to be sure that your models are defined in terms of Unicode data types, not byte string data types.我喜欢建议您检查
models.py
以确保您的模型是根据 Unicode 数据类型而不是字节字符串数据类型定义的。 Update : you say models.py is empty, so that won't be much help.更新:你说 models.py 是空的,所以不会有太大帮助。
Character handling, and Unicode vs byte strings, are handled differently in Python 2 and Python 3. Which version of Python are you using?字符处理以及 Unicode 与字节字符串在 Python 2 和 Python 3 中的处理方式不同。您使用的是哪个版本的 Python? Update : Python 2.7.6, thanks.
更新:Python 2.7.6,谢谢。 The Unicode HOWTO I linked to above is for Python 2.7.x.
我上面链接的Unicode HOWTO适用于 Python 2.7.x。
If you make sure your code handles strings as Unicode throughout, unless you really want a byte string, that will likely fix this problem.如果您确保您的代码始终将字符串作为 Unicode 处理,除非您真的想要一个字节字符串,这可能会解决这个问题。
Update : Consider modifying your template to give you debugging information.更新:考虑修改您的模板以提供调试信息。 Try something like these expressions, to see what's really in
managed_accounts
:尝试类似这些表达式,以查看
managed_accounts
的真正内容:
</thead>
<tr><td>managed_accounts is: {{ repr(managed_accounts) }}</tr></td>
{% for account in managed_accounts %}
<tr>
{% for field in account %}
<td>{{ type(field) }}, {{ repr(field) }}</td>
{% endfor %}
</tr>
{% endfor %}
[ Updated in response to multiple updates from original poster.] [更新以响应原始海报的多次更新。]
your problem is same with mine,just encode question, you can add bellow code in your views.py of django project:你的问题和我的一样,只是编码问题,你可以在django项目的views.py中添加波纹管代码:
1. #coding=utf-8
2. import sys
3. reload(sys)
4. sys.setdefaultencoding('utf-8')
my problem: Error during template rendering In template D:\\PythonProjects\\DjangoProject\\guest\\sign\\templates\\sign\\guest_manage.html, error at line 72 enter image description here我的问题:模板渲染时出错 在模板 D:\\PythonProjects\\DjangoProject\\guest\\sign\\templates\\sign\\guest_manage.html 中,第 72 行的错误在此处输入图像描述
这是属于 jinja2 模板的问题,将通过将编码设置为 utf-8 作为默认编码来解决。
sys.setdefaultencoding('utf-8')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.