luc*_*luc 3 python django unicode django-admin
在我的django应用程序中,我有一些对象导致django管理员中的相应URL为非ascii.(例如:http://mysite/admin/myapp/myclass/Présentation/)
我可以编辑对象没有任何问题,但当我保存它时,我有以下错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 24: ordinal not in range(128), HTTP response headers must be in US-ASCII format
奇怪的是,对象被正确保存到数据库中.
有谁知道Django管理员如何管理unicode?任何有助于解决此问题的信息,指针或想法将不胜感激.
提前致谢
更新:这是模型的代码
class Plugin(models.Model):
"""Some subcontent that can be added to a given page"""
class Meta:
ordering = ['ordering']
name = models.CharField(max_length=32, primary_key=True)
div_id = models.CharField(default='rightcol', max_length=32)
published = models.BooleanField(default=True,
help_text=_("If this is not checked, it is not displayed on the page."))
ordering = models.IntegerField(default=1,
help_text=_("plugins are sorted with this number in ascending order"))
content = models.TextField(blank=True)
registration_required = models.BooleanField(_('registration required'),
help_text=_("If this is checked, only logged-in users will be able to view the page."))
def __unicode__(self):
return u"%s -- %s" % (self.name, self.div_id)
Run Code Online (Sandbox Code Playgroud)
更新:很明显,URL中不建议使用非ascii字符.这是我的问题的原因,我已经改变了.
有没有人知道Django管理员使用什么来构建对象的URL.我猜这是主键.这样对吗?有没有办法强制Django使用其他东西并安全地检索对象?
我很确定你的数据库可能正在使用latin1编码.Django假设您将所有内容设置为unicode(utf8).
要查看它,请进入MySQL Shell并键入:
mysql> show variables like 'char%';
Run Code Online (Sandbox Code Playgroud)
如果你看到一堆latin1(或任何其他编码不是utf8,二进制除外),你必须这样做:打开my.cnf并查找该[mysqld]部分.确保之后tmpdir = /tmp,您有以下几行:
default-character-set=utf8
collation_server=utf8_unicode_ci
character_set_server=utf8
skip-external-locking
skip-character-set-client-handshake
Run Code Online (Sandbox Code Playgroud)
重启服务器.
您必须重新创建或手动编辑所有数据库和表的编码,更改my.cnf只会影响将要创建的数据库.
希望我帮了.
编辑:顺便说一句,你在哪个Django版本?似乎这是一个修复在1.1上的错误:http://code.djangoproject.com/ticket/10267
| 归档时间: |
|
| 查看次数: |
5866 次 |
| 最近记录: |