kaidentity kaidentity - 4 months ago 29
Python Question

Django: Umlaut problems in admin page

I went through the django tutorial https://docs.djangoproject.com/en/1.9/intro/tutorial01/, created a couple of models. Some of the models have German labels with umlauts:

Model with umlaut

When I try to link this item to another item in the UI (or even when I try to edit the item itself in order to replace the umlaut by a ascii-7-bit character) I'm getting

'ascii' codec can't encode character u'\xfc' in position 1: ordinal not in range(128)


enter image description here

I didn't edit one single code line so this can hardly be my mistake.... What needs to be done to make this work with sth else than English? I thought this was supporting utf-8 out of the box...

Thanks.

Answer

Let's say you have a simple model with title attribute. You have to encode that title to utf-8, something like this should work.

class MyModel(models.Model):
    title = models.CharField(max_length=255)

    def __str__(self):
        return self.title.encode('UTF-8')

    def __repr__(self)
        return self.title.encode('UTF-8')
Comments