在我的模型中,我有一个像这样定义的字段:
name = models.CharField(max_length = 50)
然后在管理面板中,如果我尝试插入一个名称包含'č','š','ž'等字符的记录,我会得到UnicodeEncodeError。
'ascii' codec can't encode character u'\u017e' in position 3: ordinal not in range(128)
这是什么? django为什么不使用utf-8作为一切?
答案 0 :(得分:1)
Django使用utf-8
作为一切。我想错误可能出现在您模型的__unicode__()
方法中。
您应始终对所有文本数据使用u'
前缀。所以,如果你写这样的东西:
def __unicode__(self):
return 'Model: %s' % self.name
然后您需要将其更改为:
def __unicode__(self):
return u'Model: %s' % self.name