在Django管理员中编码 [英] Encoding in Django admin

查看:108
本文介绍了在Django管理员中编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模型中,我有一个如下定义的字段:

In my model I have a field defined like this:

名称= models.CharField(max_length = 50)

name = models.CharField(max_length=50)

然后在管理面板中,如果我尝试插入名称包含'č','š','ž'等字符的记录,则会收到UnicodeEncodeError。

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)

这是什么? django为什么不对所有内容都使用utf-8?

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

推荐答案

Django使用 utf-8 。我想错误可能出在您模型的 __ unicode __()方法中。

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

您应该始终使用所有文本数据的 u'前缀。因此,如果您这样写:

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

这篇关于在Django管理员中编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆