django 字元編碼出錯解決方案

來源:互聯網
上載者:User

學習limodou的step by step,根據第七章的例子,要完成一個address book。 os :debian etch locale: zh_CN.UTF-8 django 1.0 python2.4.4 address/models.py 內容如下 #coding=utf-8 2 3 from django.db import models 4 5 # Create your models here. 6 7 class Address(models.Model): 8 name = models.CharField('姓名',max_length=20,unique=True) 9 gender = models.CharField('性別',choices=(('M','男'),('F','女')), 10 max_length=1 ) 11 telephone = models.CharField('電話',max_length=20) 12 mobile = models.CharField('手機',max_length=11) 13 14 15 class Admin: 16 pass 在admin的介面下可以完成address對象的添加和儲存,點擊Address object,可以 把記錄內容正確顯示出來。接著根據例子,在給class Address加入 def __str__(self): return self.name 在admin的介面中可以看到address object的名字顯示出來,但是點擊名字要顯示 內容時出現如下錯誤:   TemplateSyntaxError at /admin/address/address/1/ Caught an exception while rendering: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128) Original Traceback (most recent call last):   File "/usr/lib/python2.4/site-packages/django/template/debug.py", line 71, in render_node     result = node.render(context)   File "/usr/lib/python2.4/site-packages/django/template/debug.py", line 87, in render     output = force_unicode(self.filter_expression.resolve(context))   File "/usr/lib/python2.4/site-packages/django/template/__init__.py", line 559, in resolve     new_obj = func(obj, *arg_vals)   File "/usr/lib/python2.4/site-packages/django/template/defaultfilters.py", line 31, in _dec     args[0] = force_unicode(args[0])   File "/usr/lib/python2.4/site-packages/django/utils/encoding.py", line 52, in force_unicode     s = unicode(str(s), encoding, errors) UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128) 解決方案:__str__() 改成 __unicode__() Django升級到1.0之後有一些變化,還有那個class Admin也不再用於產生管理介面了 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.