Home:ALL Converter>UnicodeDecodeError when using django forms

UnicodeDecodeError when using django forms

Ask Time:2015-03-05T03:20:35         Author:JSNoob

Json Formatter

I am trying to render a form in django and encounter a UnicodeDecodeError:'ascii' codec can't decode byte 0xe7

below is my 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')

also, my 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

I also included #coding: utf-8 in each file but that is not solving the problem


below is the error message:

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

Author:JSNoob,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/28863024/unicodedecodeerror-when-using-django-forms
elethan :

I don't have much experience with Django, but I have dealt with that exception a lot when working with Chinese text in Python. Usually you need to use decode() with a Chinese codec. I have found that 'gb18030' works most frequently. So e.g., if you have a variable foo that holds Chinese strings, try foo.decode('gb18030')",
2015-03-19T14:27:08
yy