2

I have problems width encoding characters in (html) template. In my database there are results like :

  1. Čaša
  2. Šišanje

That words are in Croatian language. In my settings.py where LANGUAGE_CODE I have set : LANGUAGE_CODE = 'hr-HR' also tryd with LANGUAGE_CODE = 'hr'. The problem is when database returns record in forms:

category = forms.ModelChoiceField(queryset=Category.objects.all().filter(type_id="2"), widget=forms.Select(attrs={'class': 'selectpicker'}), label=('Kategorija'), initial='1', ) 

And all record for that field must be render in HTML page, as selectpicker. When I try to load that page I get this error:

  1. Exception Type: UnicodeEncodeError
  2. Exception Value: 'ascii' codec can't encode character u'\u0107' in position ordinal not in range(128)

Python version : 2.7.9

Django version : 1.7.7

In my models.py, the model is :

class Category(models.Model): id = models.AutoField(primary_key=True) type_id = models.ForeignKey('CategoryType') name = models.CharField(max_length=255) def __str__(self): return str(self.name) 

I have also try with:

 class Category(models.Model): id = models.AutoField(primary_key=True) type_id = models.ForeignKey('CategoryType') name = models.CharField(max_length=255) def __str__(self): return self.name 

Do you know any easy way to solve this problem, I know python 3 have better utf encoding, but I wanna stay on 2.7.

4 Answers 4

3

Change your __str__ method to __unicode__.

The exception you're seeing comes from mixing encoded bytestrings with Unicode objects. To do that, Python 2.x tries to implicitly encode the Unicode objects using the default encoding and raises an exception if it can't. Thus, when your default encoding is ascii, u'foo' + 'bar' works, but u'foo\u270c' + 'bar' does not.

In Django, CharFields on model instances return Unicode objects. In Python 2.x, the __str__ method is expected to return an encoded bytestring rather than a Unicode object, so by returning name un-encoded you're causing Django to mix Unicode objects with bytestrings.

You could also fix it by explicitly encoding name in the __str__ method, but that's the default behavior if __unicode__ is correctly defined.

Sign up to request clarification or add additional context in comments.

Comments

2

When using Python 2.7, you need to override your Model's __unicode__'s methods, not __str__. Django will take care of unicode decoding using UTF8.

Comments

1
import sys reload(sys) sys.setdefaultencoding('utf-8') 

Have you tried this?

3 Comments

Ooooooo it works :D. Thak you soo much! @skyline75489
This is just hiding the problem. See mail.python.org/pipermail/python-dev/2009-August/091406.html for some discussion of possible side effects.
it's an awesome quick fix, but according to Peter's link this might have some very ugly side effects, tnx tho
1

This is a late answer, but for those using django under python 2.x consider following django's recommended way to deal with __str__ and __unicode__.

This not only favors portability of your django project from python 2.x to 3.x but it should fix most of the issues associated to encoding.

Here is how the models.py should be adapted to follow django's recommended way using a decorator:

from __future__ import unicode_literals from django.utils.encoding import python_2_unicode_compatible @python_2_unicode_compatible class Category(models.Model): id = models.AutoField(primary_key=True) type_id = models.ForeignKey('CategoryType') name = models.CharField(max_length=255) def __str__(self): return self.name 

Hope that is helpful and happy coding!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.