django 上传一个带有中文名的照片的时候,出现了以下错误:“'ascii' codec can't encode characters in position 50-52: ordinal not in range(128)”

gq2018 发布于 2013/09/07 11:57
阅读 1K+
收藏 0

django 上传一个带有中文名的照片的时候,出现了以下错误:“'ascii' codec can't encode characters in position 50-52: ordinal not in range(128)”

 

Traceback:
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  25.                 return view_func(request, *args, **kwargs)
File "/var/www/html/www.gq2018.com/gqinfo/views.py" in info_post
  65.             newinfo.save()
File "/usr/lib/python2.6/site-packages/django/db/models/base.py" in save
  546.                        force_update=force_update, update_fields=update_fields)
File "/usr/lib/python2.6/site-packages/django/db/models/base.py" in save_base
  650.                 result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/usr/lib/python2.6/site-packages/django/db/models/manager.py" in _insert
  215.         return insert_query(self.model, objs, fields, **kwargs)
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in insert_query
  1675.     return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py" in execute_sql
  936.         for sql, params in self.as_sql():
File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py" in as_sql
  894.                 for obj in self.query.objs
File "/usr/lib/python2.6/site-packages/django/db/models/fields/files.py" in pre_save
  250.             file.save(file.name, file, save=False)
File "/usr/lib/python2.6/site-packages/django/db/models/fields/files.py" in save
  86.         self.name = self.storage.save(name, content)
File "/usr/lib/python2.6/site-packages/django/core/files/storage.py" in save
  47.         name = self.get_available_name(name)
File "/usr/lib/python2.6/site-packages/django/core/files/storage.py" in get_available_name
  73.         while self.exists(name):
File "/usr/lib/python2.6/site-packages/django/core/files/storage.py" in exists
  243.         return os.path.exists(self.path(name))
File "/usr/lib/python2.6/genericpath.py" in exists
  18.         st = os.stat(path)

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

 

 

我试了:

 

#encoding=utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

 

还按这上面试了: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 不知道在哪里加?

我直接在/etc/httpd 里建了一个envvars 然后加上上面的代码。然后重启服务器还是不管用。

我是用的django1.52+python2.6  +mod_wsgi apache centos  这个还有别的什么办法处理不?

 

加载中
0
伊藤熊吉
伊藤熊吉
envvars  指的是环境变量
0
gq2018
gq2018

那我要是在linux 下的apache 上加上:

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

应该加在哪里?会起作用呢?

0
ntsai
ntsai

django 对中文文件上传不太友好,我是自己写个重命名方法存取图片的。django部署别用APACHE,太差了,NGX+ uwsgi 是王道,必须LINUX。 WINDOWS 下偶尔会遇到编码问题

0
502BadGateway
502BadGateway

试试写一个sitecustomize.py 到python  的site-packages 目录下,内容是:

#!/usr/bin/env python
# coding: utf8  
import sys  
reload(sys)  
sys.setdefaultencoding('utf8')

0
gq2018
gq2018

引用来自“becareful”的答案

试试写一个sitecustomize.py 到python  的site-packages 目录下,内容是:

#!/usr/bin/env python
# coding: utf8  
import sys  
reload(sys)  
sys.setdefaultencoding('utf8')

试了。写上还是老样子啊。。。。
0
tianma8023
tianma8023
题主你好,我遇到了跟你一样的问题,请问您解决了没有?
tianma8023
tianma8023
刚刚找到了对应的位置,如果是Ubuntu的话,配置文件在 /etc/apache2/envvars,如果是Centos的话,配置文件在 /etc/sysconfig/httpd, 终于找到了 = = , 这个问题真是困扰了我好久,谢谢题主提供的思路 ^_^
返回顶部
顶部