0

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
JSNoob
  • 1,477
  • 2
  • 18
  • 31

2 Answers2

1

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')

elethan
  • 16,408
  • 8
  • 64
  • 87
0

I know this is a bit old, but I was able to solve a similar error, using in top of forms.py the lines:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

A similar error was reported here: UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)

mseromenho
  • 163
  • 7