Django出现的'ascii' codec can't encode characters in position...的解决办法

news/2025/2/26 3:17:24

昨天买了服务器空间,由于服务器在国外,操作系统是英文版的Ubuntu11,多多少少会遇到编码的问题

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

原先以为是没有安装语言包,所以转了一下中文简体语言包:
sudo apt-get install language-pack-zh-hans

执行后还是不行,这下没头绪了

网上搜了下,尝试了以下几个解决办法:

1. 加上

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

结果还是失败

2. 去Python27的site-package加文件,注明:

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

结果还是不行


解决办法:

最后首先在django官网上看到一个bug,说的就是此事:https://code.djangoproject.com/ticket/6009

随后在stackoverflow上找到了解决办法:

http://stackoverflow.com/questions/3715865/unicodeencodeerror-ascii-codec-cant-encode-character

1. 配置Apache:

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

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

2.配置Nginx:

在nginx.config的http{..}中间加上:

charset  utf-8;

最后重启apache,重启nginx,一切解决了,耶!



版权所有。转载本BLOG内任何文章,请以超链接形式注明出处。

http://www.niftyadmin.cn/n/3649177.html

相关文章

ios pusher使用_如何使用Laravel和Pusher通道创建Web通知

ios pusher使用Many web applications include an in-app notification system that will notify you instantly when someone carries out an action related to you or your account. On Facebook, you will be notified when someone likes your status, or when someone co…

有些像穴道被打通之前的周星驰

借用别人的一句话(http://ma-yue.net/index.php?p222):有时候我觉得我们都有些像《功夫》中穴道被打通之前的周星驰:嗓门很大,梦想很多,但是除了开锁之技,却没有什么真正可以服人的“功夫”。不练会如来神掌之类的硬功…

在django中使用memcache

1,安装memcachesudo apt-get install memcached2,安装python的memcached支持wget ftp://ftp.tummy.com/pub/python-memcached/python-memcached-latest.tar.gz解压,进入目录python setup.py install3,配置memcached由于memcached不能用root帐号启动服务&…

简单的实现播放视频

获取本地视频 public void getDataFromLocal() {mediaItems new ArrayList<>();new Thread() {Overridepublic void run() {super.run();ContentResolver resolver mContext.getContentResolver();Uri uri MediaStore.Video.Media.EXTERNAL_CONTENT_URI;String[] objs…

Delphi ListView快速操作通用实现

Delphi ListView快速操作通用实现作者&#xff1a;成晓旭众所周知&#xff0c;Delphi ListView类直接进行Add、Update、Delete操作的速度是比较慢的&#xff0c;尤其是当数据量较大时&#xff0c;比如数据量达到5000、10000、50000时&#xff0c;速度真是可以说是“慢得惊人”。…

anime.js_如何使用CSS,anime.js和segment.js与微交互构建下载按钮

anime.js引言 (Introductions) In user experience design, microinteractions are small moments of feedback that help a user navigate an interface. Often, microinteractions are made with subtle animation in website design. 在用户体验设计中&#xff0c; 微交互是…

解决amazon RDS 出现“Illegal mix of collations (latin1_swedish_ci,IMPLICIT)”的解决办法

今天遇到一个问题&#xff0c;我把数据库从EC2上安装的postgres替换为了amazon提供的RDS&#xff0c;之后在保存中文的时候出现错误&#xff1a; Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) 出现这个问题的原因是因为RDS的mys…

Android gallery 3D效果

在看了iOS上面的CoverFlow后&#xff0c;感觉效果真的不错&#xff0c;就想在android上面实现一个&#xff0c;这个程序在网上参考了一此核心的代码&#xff0c;当然我添加了一些其他的东西&#xff0c;废话不多说&#xff0c;先看效果&#xff0c;不然就是无图无真相。 Demo下…