我對unicode的本質有點模糊不清,但我不確定所有的部分如何組合在一起。在管理頁面中顯示特定實例時出現錯誤。管理頁面上的django unicode錯誤
Caught UnicodeEncodeError while rendering: 'ascii' codec can't encode character u'\u2019' in position 29: ordinal not in range(128)
這裏是我的模型:
class Proposal(models.Model):
project = models.ForeignKey(Project)
dateCreated = models.DateTimeField(editable=False)
xml = models.TextField(max_length=1000000)
def __str__(self):
return str('Proposal for: %s' % self.project.name)
我已經到我的MySQL數據庫和驗證數據庫,表和列都整理爲utf8_unicode_ci,所以我不明白爲什麼頁面正在嘗試呈現ascii。查看各種論壇和文檔,我看到提及的str和unicode函數,但它們似乎沒有任何關係,因爲實例列表在管理頁面顯示正常。它只是顯示導致問題的實際實例表單。
下面是一些示例XML我從phpMyAdmin的拉...
<?xml version="1.0" encoding="UTF-8"?>
<proposal>
<section title="OVERVIEW">
<section title="Introduction">
<text>
This proposal is not in the system because it was completed as an agreement in Word previous to us getting this application up and running. Please refer to the attachments in this project for documentation or to see the agreement.
</text>
</section>
</section>
</proposal>
我甚至試過刻意排除XML(我不能從長遠來看,這樣做,因爲我想它喜歡可以在管理部分編輯),但我仍然得到相同的錯誤,所以我甚至不相信xml甚至是問題。如果xml不是問題,我不知道還有什麼可以保持這個頁面不被顯示。
class ProposalAdmin(admin.ModelAdmin):
exclude = ('xml',)
admin.site.register(Project)
Thanks for the encode()trick - ps:it should'encode('ascii','replace')' –
@Yuji Tomita,我想你可以兩種方式調用它。關鍵字參數和東西:http://docs.python.org/reference/expressions.html#calls –
'編碼()'沒有關鍵字參數 - 至少在2.6 –