2010-01-06 42 views
3

在我的django應用程序中,有一些對象導致django admin中的相應URL爲非ascii。 (例如:http://mysite/admin/myapp/myclass/Présentation/保存django中的對象時出現unicode錯誤admin

我可以編輯的對象沒有任何問題,但是當我保存它,我有以下錯誤:

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嗎?任何信息,指針或想法,可以幫助解決這個問題,將不勝感激。

在此先感謝

更新:這裏是Model

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) 

更新的代碼: 是十分明顯的非ASCII字符不會在URL建議。這是我的問題的原因,我改變了這一點。

有沒有人知道Django管理員使用什麼來構建對象的URL。我猜這是主要關鍵。這樣對嗎?有沒有辦法強制Django使用別的東西並安全地檢索對象?

+0

你可以發佈你的Présentation模型嗎? – 2010-01-06 10:09:03

回答

0

我現在使用默認ID作爲我的模型的每個類的主鍵。因此,從管理站點訪問對象時,我沒有禁止URL中的字符。

我建議保持默認的id作爲主鍵在大多數情況下

6

我很確定你的數據庫可能使用latin1編碼。 Django假設你將所有設置設置爲unicode(utf8)。

檢查了這一點,進入mysql外殼和類型:

mysql> show variables like 'char%'; 

如果你看到一堆LATIN1的(或任何其他編碼不是UTF8,除了binary),你必須執行此操作: 打開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 

重新啓動服務器。

您必須重新創建或手動編輯所有數據庫和表的編碼,更改my.cnf隻影響將創建的數據庫。

希望我幫了忙。

編輯:順便說一句,你在哪個Django版本上?看起來這是固定在1.1的錯誤:http://code.djangoproject.com/ticket/10267

+0

我會檢查與我的主機提供商。我有 | character_set_client | latin1 | | character_set_connection | latin1 | | character_set_database | utf8 | | character_set_filesystem |二進制| | character_set_results | latin1 | | character_set_server | latin1 | – luc 2010-01-07 06:30:52

+0

如果他們不改變它,你可以重新創建你的數據庫,設置一切utf8 – Clash 2010-01-07 11:29:24

+0

根據我的主機提供商,這個pb是不相關的數據庫編碼。我認爲他是對的,因爲這個pb是零星的。感謝您的幫助 – luc 2010-01-07 14:24:31

1
模型中的

,你試圖把

class Model(models.Model): 
    def __unicode__(self): 
     return self.name ## having a model object named "name" 

不知道這是否是你所searhing的答案,但你有沒有試過?

+0

我的模型有一個unicode。 Django不使用__unicode__來構建url。我認爲它將無法安全地檢索該對象。我猜它使用主鍵。謝謝你的幫助 – luc 2010-01-07 16:27:54

2

This link救了我的
天 你必須添加你的管理員在models.py Unicode支持:

class MyModel (models.Model): 
    some_field = models.CharField(max_length=1000) 
    def __unicode__(self): 
     return u'%s'%(self.some_field) 

有可能是其他問題,如:您的系統編碼是不是utf8,您的數據庫編碼是不是utf8和...哪在提供的鏈接中提到!

相關問題