Django报错UnicodeEncodeError: 'ascii' codec can't encode... 的解决方法

我在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 项目的迁移过程中遇到 `ModuleNotFoundError: No module named 'model.config'` 的错误时,这通常意味着 Django 无法找到名为 `model.config` 的模块。以下是可能的原因和解决步骤: 1. **路径问题**[^1]: - 检查您的 `settings.py` 文件中的 `INSTALLED_APPS` 列表,确保包含了 `'model.config'` 这个应用。如果是导入错误,应确保已正确添加到这个列表里。 ```python INSTALLED_APPS = [ ... 'model.config', # 如果还没添加,这里需要加上 ... ] ``` 2. **模块结构**: - 确认 `model.config` 是否已经存在于项目的正确的文件夹结构中(通常是 `app_name/config.py` 或者 `app_name/models.py`),并且该目录已经在项目的 `PYTHONPATH` 中。 3. **导入语句**: - 确保在需要使用的文件中,导入 `'model.config'` 的方式是正确的。如果是在某个视图或管理器中,可能是: ```python from model.config import YourModelClass ``` 或者 ```python from .config import YourConfigFunction ``` 4. **名称冲突**: - 如果另一个模块已经占用了相同的名称,尝试更改 `model.config` 的名称,以避免导入冲突。 5. **清理缓存**: - 清理并重新加载 Django 的开发服务器 (`python manage.py runserver --clear-cache`) 可能有助于解决因缓存导致的导入问题。 6. **安装依赖**: - 确保已经正确安装了 `model.config` 所需的所有依赖,特别是如果它是一个第三方库,需要通过 pip 安装。 如果以上步骤都不能解决问题,检查是否有其他同名模块干扰,或者是否在项目的 setup.py 文件中正确地指定了模块的入口点。另外,检查 `model.config` 是否已经被正确地添加到了项目的版本控制系统(如 Git)中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值