【发布时间】:2019-01-11 10:28:11
【问题描述】:
我们有一个奇怪的问题。如果我们在 Django 中使用非 ascii 符号,它会在列表中正常显示它们,
但如果我们尝试输入该项目,它会出现以下错误:
sys.setdefaultencoding("utf-8") 有帮助,但有人认为这是错误的方法 (Why should we NOT use sys.setdefaultencoding("utf-8") in a py script?)。我怎样才能正确解决它?为什么列表正确显示?任何想法表示赞赏!
完整的追溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/enter/event/23/change/
Django Version: 1.9.4
Python Version: 2.7.10
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar',
'enter']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware']
Template error:
In template /Library/Python/2.7/site-packages/django/contrib/admin/templates/admin/change_form.html, error at line 21
ascii 11 : {% block coltype %}colM{% endblock %}
12 :
13 : {% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} change-form{% endblock %}
14 :
15 : {% if not is_popup %}
16 : {% block breadcrumbs %}
17 : <div class="breadcrumbs">
18 : <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
19 : › <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a>
20 : › {% if has_change_permission %}<a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst }}</a>{% else %}{{ opts.verbose_name_plural|capfirst }}{% endif %}
21 : › {% if add %}{% trans 'Add' %} {{ opts.verbose_name }}{% else %} {{ original|truncatewords:"18" }} {% endif %}
22 : </div>
23 : {% endblock %}
24 : {% endif %}
25 :
26 : {% block content %}<div id="content-main">
27 : {% block object-tools %}
28 : {% if change %}{% if not is_popup %}
29 : <ul class="object-tools">
30 : {% block object-tools-items %}
31 : <li>
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
174. response = self.process_exception_by_middleware(e, request)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
172. response = response.render()
File "/Library/Python/2.7/site-packages/django/template/response.py" in render
160. self.content = self.rendered_content
File "/Library/Python/2.7/site-packages/django/template/response.py" in rendered_content
137. content = template.render(context, self._request)
File "/Library/Python/2.7/site-packages/django/template/backends/django.py" in render
95. return self.template.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
206. return self._render(context)
File "/Library/Python/2.7/site-packages/django/test/utils.py" in instrumented_test_render
92. return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
173. return compiled_parent._render(context)
File "/Library/Python/2.7/site-packages/django/test/utils.py" in instrumented_test_render
92. return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
173. return compiled_parent._render(context)
File "/Library/Python/2.7/site-packages/django/test/utils.py" in instrumented_test_render
92. return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)
File "/Library/Python/2.7/site-packages/django/template/defaulttags.py" in render
326. return nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
69. result = block.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)
File "/Library/Python/2.7/site-packages/django/template/defaulttags.py" in render
326. return nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
1043. output = self.filter_expression.resolve(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in resolve
736. new_obj = func(obj, *arg_vals)
File "/Library/Python/2.7/site-packages/django/template/defaultfilters.py" in _dec
47. args[0] = force_text(args[0])
File "/Library/Python/2.7/site-packages/django/utils/encoding.py" in force_text
80. s = six.text_type(bytes(s), encoding, errors)
Exception Type: UnicodeEncodeError at /enter/event/23/change/
Exception Value: 'ascii' codec can't encode character u'\xe9' in position 29: ordinal not in range(128)
【问题讨论】:
-
请发布完整的回溯(作为文本)。还要检查您的
settings.DEFAULT_CHARSET和您的数据库编码(表和客户端)。 -
你的
settings.DEFAULT_CHARSET是什么?您的模型字段标签或 help_text 中是否有重音字符?您是仅在某些记录上还是在所有记录上都存在此问题?如果尝试从管理员创建新事件,您是否有问题? -
@scharette django 使用“unicode 三明治”模式,因此模板需要 unicode 字符串并负责对其进行编码。
-
@Arkady 不检查您的代码就无法判断 - 这里有很多因素在起作用,包括您是否使用
from __future__ import unicode_literals和/或您是否正确使用u""unicode 前缀,如果您的源代码中有# -*- coding: <encoding_name>标头,您的__unicode__方法是如何实现的以及诸如此类的... wrt/settings.DEFAULT_CHARSET如果未设置,则默认为utf-8,并且由于您的错误消息明确提及在 ascii 编解码器中,它确实不是罪魁祸首。 -
作为一般规则:如果您想安全起见,您希望始终只在代码中使用 unicode,特别是对于文字字符串。 Django 本身在系统入口点(HTTP 查询、数据库查询等)将所有内容转换为 unicode。