Exception Value: 'ascii' codec can't encode characters in position 55-56: ordinal not in range(128)

gq2018 发布于 2013/09/06 15:58
阅读 863
收藏 0

如果在models.py 里这样定义:

models.py:
infopic = models.FileField(upload_to='./pic/%Y/%m/%d/%H/', blank=True, null=True)

fomrs.py:
infopic = forms.FileField(label=u'相关图片', required=False)


vimews.py:
 if form.is_valid():
            newinfo = Gqinfo(
                title = form.cleaned_data['title'],
                content = form.cleaned_data['content'],
                infopic = form.cleaned_data['infopic'],
                user = request.user,
                        )
            newinfo.save()

 

如果上传的图片里路径或图片的名字是中文的。比如: 中国.jpg 中国123.jpb

在发布信息的时候就会出错。提示:

 

 

Environment:


Request Method: POST
Request URL: http://www.aaa.com/post

Exception Type: UnicodeEncodeError at /post/
Exception Value: 'ascii' codec can't encode characters in position 55-56: ordinal not in range(128)

 

 

UnicodeEncodeError at /post/
'ascii' codec can't encode characters in position 55-56: ordinal not in range(128)Request Method: POST
Request URL: http://www.gq.com/post/
Django Version: 1.5.2
Exception Type: UnicodeEncodeError
Exception Value: 'ascii' codec can't encode characters in position 55-56: ordinal not in range(128)
Exception Location: /usr/lib/python2.6/genericpath.py in exists, line 18
Python Executable: /usr/bin/python
Python Version: 2.6.6
Python Path: ['/usr/lib/python26.zip',
 '/usr/lib/python2.6',
 '/usr/lib/python2.6/plat-linux2',
 '/usr/lib/python2.6/lib-tk',
 '/usr/lib/python2.6/lib-old',
 '/usr/lib/python2.6/lib-dynload',
 '/usr/lib/python2.6/site-packages',

 

 

 

难道是那图片的路径不能是中文的吗?

 

加载中
0
sToa
sToa

在源码头部加入下面的代码试试

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

0
王瑞平
王瑞平

中文字符UNICODE编码都大于128

转码utf-8

0
gq2018
gq2018

引用来自“prehisle”的答案

在源码头部加入下面的代码试试

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

我加了啊。不管用的

0
gq2018
gq2018

引用来自“王瑞平”的答案

中文字符UNICODE编码都大于128

转码utf-8

怎么个转法?能详细说一下不?
0
gq2018
gq2018

我在网上找到了解决办法。http://www.07net01.com/linux/Djangochuxiande_ascii__codec_can_t_encode_characters_in_positio_11063_1345791548.html

但是 他说在

 

在/etc/apache2/envvars文件加上:

export LANG='en_US.UTF-8'export LC_ALL='en_US.UTF-8'

我是用的centos 没找到envvars 不知道在哪里加?

 

 

返回顶部
顶部