简体   繁体   中英

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:

带有变音符号的模型

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)

在此处输入图片说明

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM