我目前正在使用Django-countries 获取表单字段的 ISO 国家/地区名称列表。它在我的网站上显示没有错误,但在 Django 的管理网站上,由于存在非 ASCII 国家名称,它会导致上述错误:
Unicode 错误提示:无法编码/解码的字符串是:itan Åland
我读过类似的问题可以使用encode('utf-8') 属性来解决,但是如果不接触 Django Admin 本身,这似乎不太可行。如果有人能解决这个问题,我将不胜感激。
Models.py(消息):
从 __future__ 导入 unicode_literals
从 django.db 导入模型
从 auth.models 导入用户
从 tourlisting.models 导入旅游
类行程(models.Model):
start_date = models.DateField()
end_date = models.DateField()
def __str__(self):
return "行程" + str(self.id)
类 TourActivity(models.Model):
start_time = models.DateTimeField()
end_time = models.DateTimeField()
描述 = models.CharField(max_length=500, default='')
位置 = models.CharField(max_length=200)
行程 = models.ForeignKey(行程)
类线程管理器(模型。管理器):
def create_thread(自我,旅游,行程,旅行者):
线程 = self.create(tour=tour, itinerary=itinerary, traveller=traveller)
返回线程
类线程(模型。模型):
tour = models.ForeignKey(Tour)
itinerary = models.ForeignKey(Itinerary, null=True)
traveler = models.ForeignKey(User)
对象 = 线程管理器()
def __str__(self):
返回“线程”+ str(self.id)
类消息(模型。模型):
消息 = 模型.TextField()
发件人 = models.ForeignKey(用户,related_name='sent_messages')
线程 = 模型.ForeignKey(线程)
time_sent = models.DateTimeField(auto_now_add=True)
time_read = models.DateTimeField(null=True, blank=True)
Models.py(旅游列表):
从 __future__ 导入 unicode_literals
从 django.db 导入模型
从 auth.models 导入用户配置文件,用户
从 djmoney.models.fields 导入 MoneyField
从 django.utils 导入时区
从 django_countries.fields 导入 CountryField
导入日期时间
类类别(模型。模型):
name = models.CharField(max_length=100, unique=True)
def __str__(self):
返回 self.name
类旅游(模型。模型):
tour_name = models.CharField(max_length=100, default='')
tour_desc = models.CharField('Tour Description', max_length=10000, default='')
tour_guide = models.ForeignKey(UserProfile)
tour_length = models.IntegerField('游览长度(天)',默认 = 3)
国家= CountryField(默认='',max_length=100)
city = models.CharField(max_length=1000, default='')
ppl_min = models.IntegerField('最少旅客人数', default=1)
ppl_max = models.IntegerField('最大数量,如果旅行者',默认 = 10)
price = MoneyField(max_digits=6,decimal_places=2, default_currency='USD', default=50)
类别=模型.ForeignKey(类别)
pub_date = models.DateField('发布日期', auto_now_add=True)
active = models.BooleanField(default=True)
已删除 = models.BooleanField(default=False)
def __str__(self):
返回“{0} by {1}”.format(self.tour_name, self.tour_guide)
def was_listed_recently(self):
返回 self.pub_date >= timezone.now() - datetime.timedelta(days=7)
堆栈跟踪:
环境:
请求方法:GET
请求网址:http://localhost:8000/admin/messaging/thread/2/change/
Django 版本:1.9.2
Python版本:2.7.10
已安装的应用程序:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'django.contrib.staticfiles',
'消息',
'crispy_forms',
'djmoney',
'django_countries',
'草垛',
'pysolr',
'django_tables2',
'phonenumber_field',
'django_extensions')
已安装的中间件:
('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',
'django.middleware.security.SecurityMiddleware')
模板错误:
在模板 /usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/admin/change_form.html 中,第 33 行出错
ascii 23 : {% endblock %}
24 : {% endif %}
25:
26 : {% 块内容 %}
27 : {% 块对象工具 %}
28 : {% if change %}{% if not is_popup %}
29:
30 : {% 块对象工具项 %}
31:
32 : {% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
33 : {% trans "历史" %}
34:
35 : {% if has_absolute_url %}
{% trans "现场查看" %}{% endif %}
36 : {% 端块 %}
37:
38 : {% endif %}{% endif %}
39 : {% 端块 %}
40 : {% csrf_token %}{% 块 form_top %}{% endblock %}
41:
42 : {% if is_popup %}{% endif %}
43 : {% if to_field %}{% endif %}
追溯:
get_response 中的文件“/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py”
174. 响应 = self.process_exception_by_middleware(e,请求)
get_response 中的文件“/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py”
172. 响应 = response.render()
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/response.py”
160. self.content = self.rendered_content
渲染内容中的文件“/usr/local/lib/python2.7/dist-packages/django/template/response.py”
137. 内容 = 模板.渲染(上下文,self._request)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/backends/django.py”
95. return self.template.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
206. return self._render(context)
_render 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
197.返回self.nodelist.render(上下文)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
992.bit = node.render_annotated(上下文)
render_annotated 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
959. return self.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py”
173.返回已编译的_parent._render(上下文)
_render 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
197.返回self.nodelist.render(上下文)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
992.bit = node.render_annotated(上下文)
render_annotated 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
959. return self.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py”
173.返回已编译的_parent._render(上下文)
_render 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
197.返回self.nodelist.render(上下文)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
992.bit = node.render_annotated(上下文)
render_annotated 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
959. return self.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py”
69. 结果 = block.nodelist.render(上下文)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
992.bit = node.render_annotated(上下文)
render_annotated 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
959. return self.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py”
69. 结果 = block.nodelist.render(上下文)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
992.bit = node.render_annotated(上下文)
render_annotated 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
959. return self.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py”
220. nodelist.append(node.render_annotated(context))
render_annotated 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
959. return self.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py”
209. 返回模板.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
208.返回self._render(上下文)
_render 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
197.返回self.nodelist.render(上下文)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
992.bit = node.render_annotated(上下文)
render_annotated 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
959. return self.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py”
220. nodelist.append(node.render_annotated(context))
render_annotated 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
959. return self.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py”
220. nodelist.append(node.render_annotated(context))
render_annotated 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
959. return self.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py”
326.返回nodelist.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
992.bit = node.render_annotated(上下文)
render_annotated 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
959. return self.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py”
326.返回nodelist.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
992.bit = node.render_annotated(上下文)
render_annotated 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
959. return self.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
1049. 返回render_value_in_context(输出,上下文)
render_value_in_context 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
1026. 值 = force_text(值)
force_text 中的文件“/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py”
78. s = 6.text_type(s)
文件“/usr/local/lib/python2.7/dist-packages/django/utils/html.py”在
381. klass.__unicode__ = lambda self: mark_safe(klass_unicode(self))
__str__ 中的文件“/usr/local/lib/python2.7/dist-packages/django/forms/boundfield.py”
43. 返回 self.as_widget()
as_widget 中的文件“/usr/local/lib/python2.7/dist-packages/django/forms/boundfield.py”
101. return force_text(widget.render(name, self.value(), attrs=attrs))
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/contrib/admin/widgets.py”
291.'小部件':self.widget.render(名称,值,*args,**kwargs),
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py”
528. 选项 = self.render_options(选择,[值])
render_options 中的文件“/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py”
554. 对于 option_value,链中的 option_label(self.choices,choices):
__iter__ 中的文件“/usr/local/lib/python2.7/dist-packages/django/forms/models.py”
1110. 产生 self.choice(obj)
选择文件“/usr/local/lib/python2.7/dist-packages/django/forms/models.py”
1117.返回(self.field.prepare_value(obj),self.field.label_from_instance(obj))
label_from_instance 中的文件“/usr/local/lib/python2.7/dist-packages/django/forms/models.py”
1180. 返回智能文本(obj)
smart_text 中的文件“/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py”
42. 返回 force_text(s, encoding, strings_only, 错误)
force_text 中的文件“/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py”
80. s = Six.text_type(字节,编码,错误)
异常类型:/admin/messaging/thread/2/change/ 处的 UnicodeEncodeError
异常值:'ascii' 编解码器无法对位置 35 中的字符 u'\xc5' 进行编码:序数不在范围内(128)