问题
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