我在Dreamhost上使用python2.5 , mysql1.2.2, pil 1.1.6,做个小小的网站的时候,遇到一个问题就是。UnicodeEncodeError: 'ascii' codec can't encode..
GOOGLE,BAIDU上搜了好久都没找到答案,不过很明显这是编码的问题,知道django在处理汉字的时候出现的问题,但是有些model都没有问题,而偏偏就model里面有ForeignKey,ManytoManyField的时候就出现这样的问题。
我的model是:Product,和ProductImage:
是django admin后天操作Product的时候完全正常,无论是添加删除还是修改,但是到了ProductImage就出问题了,添加的时候,提及表单的时候出现UnicodeEncodeError,其实图片以及上传到服务器了。
后台,我就找了找两个model的差别,就在return '%s-%s' % self.name,self.product 和return self.name,我就试着把ProductImage的return '%s-%s' % self.name,self.product 改成了return self.name或者return self.product试了一下,结果成功了。但是我就是弄不明白为什么?这有什么区别?
后台在网上找到了一段资料:
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!
分享到:
相关推荐
Django,命令:pip install django Pymysql 命令:pip install pymysql 进入程序主目录,然后修改数据库地址密码:xueshenghui\settings.py里面的DATABASES参数 进入程序主目录,运行python manage.py runserver ...
主要给大家介绍了关于django启动uwsgi报错的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
Python培训基础教程:django框架搭建和安装.pptx
三、Django之使用模板并引用静态内容:jquery-3.6.0.min.js和bootstrap-3.4.1
目 录 ...17. 第十六章:集成的子框架 django.contrib 18. 第十七章:中间件 19. 第十八章:集成已有的数据库和应用 20. 第十九章:国际化 21. 第二十章:安全 22. 本站由VCC使用Django搭建。
一、安装 ... 上面命令会在桌面新建pythonDjango文件夹,在里面会生成如下图两个文件 manage.py:命令行工具,可以用多种方式与该django项目进行交互; 在pythonDjango文件夹里面有4个.py文件
资源分类:Python库 所属语言:Python 资源全名:django-oauth2-1.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
django_course 项目django1: ://django01-rickrribeiro.herokuapp.com/
前几天写的django 简易博客开发记录,贴个链接吧 django 简易博客开发 1 安装、创建、配置、admin使用 http://www.cnblogs.com/cacique/archive/2012/09/29/2707976.html django 简易博客开发 2 模板和数据查询 ...
django1.1 api from官方安装包(Django-1.1.1.tar.gz):包括Tutorial和各个模块的完整api。 鉴于csdn中已有资源(django api chm版本)打不开的原因,本人特地自行编译成html。该doc和官方提供的一摸一样,但是为...
学习Django框架时,创建一个引擎及索引时报错,具体报错如下: 执行命令: python3 manage.py rebuild_index 报如下错误: ......(前面一堆报错) File "/home/python/Django/test6/test6/urls.py", line 20, in url...
django教程:基本操作笔记整理.png
django.db.utils.OperationalError: (1045, “Access denied for user ‘账号’@’localhost’ (using password: YES)”) 错误。 这种错误指的是连接数据库时账号密码错误。 1.只需要修改setting.py文件里的...
报错:UnicodeEncodeError: 'ascii' codec can't encode characters in position x-x: ordinal not in range(128) 1.因代码中的中文注释问题的报错 django项目的.py文件文件中加入了中文注释,经常会因此报错而导致...
第十四章 集成的子框架 django.contrib 完成度 99.72% 阅读 翻译 第十七章: 中间件 完成度 95.08% 阅读 翻译 第十八章: 集成已有的数据库和应用 完成度 100.00% 阅读 翻译 第十九章: 国际化 完成度 100.00% 阅读 ...
第十四章 集成的子框架 django.contrib 完成度 99.72% 第十七章: 中间件 完成度 95.08% 第十八章: 集成已有的数据库和应用 完成度 100.00% 第十九章: 国际化 完成度 100.00% 第二十章: 安全 完成度 100.00%
django 设置settings.py,本文以mysql 为例,说明怎样配置mysql d的连接
django教程:基本操作笔记整理.rar