现在的位置: 首页 > 综合 > 正文

Django报错UnicodeEncodeError: ‘ascii’ codec can’t encode characters 之解决方法

2018年01月18日 ⁄ 综合 ⁄ 共 3355字 ⁄ 字号 评论关闭
 原先用Python 2.4 + Django 0.95 写得程序,当我把环境升级到Python 2.5 +  Django 0.97(geodjango)的时候,并且把两个表做了外键关链的情况下,发现后台添加数据,会出现错误,Python报错如下:

 [color=#FF0000]UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)[/color] 

找了好多地方,终于还是在官方code.djangoproject.com找到了答案,

引用
Porting Applications (The Quick Checklist) ¶
One
of the design goals of the Unicode branch is that very little
significant changes to existing third-party code should be required.
However, there are some things that developers should be aware of when
writing applications designed to handle international input.

A
detailed list of things you might wish to think about when writing your
code is in the unicode.txt file in the documentation directory (also
online). For the programmer on a deadline, here is the cheatsheet
version (if you only use ASCII strings, none of these changes are
necessary):

(Note (25 May 2007): Early adopters will have seen
five steps in this list. The all-important step number 3 was initially
omitted.)

1. Change the
__str__ methods on your models to be __unicode__ methods. Just change
the name. Usually, nothing else will be needed.

2. Look
for any str() calls in your code that operate on model fields. These
should almost always be changed to smart_unicode() calls (which is
imported from django.utils.encoding). In some cases, you may need to
use force_unicode() (in the same module), but starting with a global
change to smart_unicode() and then checking for problems is the "quick
fix" way. (Details of the differences between the two functions are in
unicode.txt.)
3. Change your string literals that include Python format characters to be unicode strings. For example, change this:
   

formal_name = '%s %s %s' % (title, firstname, surname)     # old version

  to this:
   

formal_name = u'%s %s %s' % (title, firstname, surname)    # new version

This
is useful for two reasons. Firstly, if the parameters contain non-ASCII
characters, you won't have an exception raised. Secondly, if any of the
parameters are objects, Python will automatically call their
__unicode__ method and convert them to the right type. The "before"
code would have resulted in the __str__ method being called instead. Of
course, this step is only a good idea if you are interpolating unicode
strings. If your parameters are bytestrings, they will not
automatically be decoded to unicode strings before being interpolated
(Python cannot read your mind). Use smart_unicode() for that purpose.

× 
Warning for Python 2.3: There is a bug in the way Python 2.3 does
string interpolation for unicode strings that you should be aware of if
your code has to work with that version of Python. In the second line
of code, above, if any of the parameters are non-basestring objects,
Python will call the __str__ method on the object, not the __unicode__
method! So, for Python 2.3-compatible code, you would need to write
something like some_string = u'This is your object: %s' %
unicode(some_object)

Note the explicit call to unicode() here to force the object to be the right type.

4.
 Use the unicode versions of the django.utils.translation.* functions.
Replace gettext and ngettext with ugettext and ungettext respectively.
There are also ugettext_lazy and ungettext_lazy functions if you use
the lazy versions.

5. Make sure your database can store all
the data you will send to it. Usually, this means ensuring it is using
UTF-8 (or similar) encoding internally.

6. Use the FILE_CHARSET setting if your on-disk template files and initial SQL files are not UTF-8 encoded.

  That is all. Enjoy!

   Python中有两种字符串,分别是一般的字符串(每个字符用8 bits表示)和Unicode字符串(每个字符用一个或者多个字节表示),它们可以相互转换。从错误提示来看同由于Python遇到了编码问题,也就是说,后台输入的数据在默认情况下是ascii编码的,那么在存入数据库的时候,Python便会报错,即使返回去看,数据库中已经插入了该条信息!

  解决方法:  因为原先写的模型代码中的方法用提 def __str__(self): 这个是旧版本中用的方法,在Django 0.96以后的版本中,应该换成 def __unicode__(self):, 这样就解决了字符串传递时出错的问题,统一编码为Unicode字符串。

抱歉!评论已关闭.