我正在尝试在django中呈现一个表单并遇到UnicodeDecodeError:'ascii'编解码器无法解码字节0xe7
下面是我的forms.py:
from models import Country, Question
class QuestionForm(forms.ModelForm):
...
country=forms.ModelMultipleChoiceField(queryset=Country.objects.all())
# The above queryset will return a list of names in Chinese
class Meta:
model=Question
field=(...,'country')
另外,我的models.py
class Country(models.Model):
country_name=models.CharField(max_length=100)
country_pic=models.ImageField(upload_to='country_pic/')
def __unicode__(self):
return self.country_name
class Question(models.Model):
country=models.ForeignKey(Country)
question_title=models.CharField(max_length=100)
def __unicode__(self):
return self.question_title
我还在每个文件中包含了#coding:utf-8但是没有解决问题
以下是错误消息:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/website/add_question/
Django Version: 1.7.4
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'website')
Installed Middleware:
('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')
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/qiaoweiliu/Desktop/FinalProject/ask/website/views.py" in add_question
64. if form.is_valid():
File "/Library/Python/2.7/site-packages/django/forms/forms.py" in is_valid
162. return self.is_bound and not bool(self.errors)
File "/Library/Python/2.7/site-packages/django/forms/forms.py" in errors
154. self.full_clean()
File "/Library/Python/2.7/site-packages/django/forms/forms.py" in full_clean
355. self._post_clean()
File "/Library/Python/2.7/site-packages/django/forms/models.py" in _post_clean
406. self.instance = construct_instance(self, self.instance, opts.fields, opts.exclude)
File "/Library/Python/2.7/site-packages/django/forms/models.py" in construct_instance
60. f.save_form_data(instance, cleaned_data[f.name])
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in save_form_data
804. setattr(instance, self.name, data)
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py" in __set__
597. self.field.rel.to._meta.object_name,
Exception Type: UnicodeDecodeError at /website/add_question/
Exception Value: 'ascii' codec can't decode byte 0xe7 in position 11: ordinal not in range(128
答案 0 :(得分:1)
我对Django没有多少经验,但是在使用Python中的中文文本时,我已经处理了很多异常。通常你需要使用带有中文编解码器的decode()。我发现' gb18030'工作最频繁。例如,如果您有一个包含中文字符串的变量foo
,请尝试foo.decode('gb18030')
答案 1 :(得分:0)
我知道这有点老了,但是我可以通过在forms.py顶部使用以下行来解决类似的错误:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
这里报告了类似的错误: UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)