Encoding in Django admin

纵饮孤独 提交于 2019-12-25 03:14:30

问题


In my model I have a field defined like this:

name = models.CharField(max_length=50)

Then in admin panel if I try to insert a record with the name that contains characters like 'č', 'š', 'ž', I get the UnicodeEncodeError.

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

What is this? Why doesn't django use utf-8 for everything?


回答1:


Django uses utf-8 for everything. I suppose that the error might be in the __unicode__() method of your model.

You should always use the u' prefix for all text data. So if you write something like this:

def __unicode__(self):
    return 'Model: %s' % self.name

then you need to change it to:

def __unicode__(self):
    return u'Model: %s' % self.name


来源:https://stackoverflow.com/questions/29431108/encoding-in-django-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!