您的位置:首页 > 运维架构 > Apache

django用apache+mod_wsgi部署后中文乱码的解决方法

2015-10-08 20:36 891 查看
主要是参考了django的官方文档:https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/modwsgi/

问题的起因是:自己通过python manage.py runserver 0.0.0.0:8082这种方式开启项目,中文不会乱码,但是楼主自己也在阿里云上部署了一个网站,通过django+apache+mod_wsgi+ubuntu这样的环境下配置的,却会出现中文乱码错误,

好几个月没有解决这个问题。

今天项目要添加上传中文文件,结果又碰到这种情况,本地服务器可以,阿里云服务器又不行,于是狠下心一定要干掉这个问题。下面无偿奉献出解决方法。

 

If you get a UnicodeEncodeError

If you’re taking advantage of the internationalization features of Django (see Internationalization
and localization) and you intend to allow users to upload files, you must ensure that the environment used to start Apache is configured to accept non-ASCII file names. If your environment is not correctly configured, you will trigger UnicodeEncodeErrorexceptions
when calling functions like the ones in os.path on
filenames that contain non-ASCII characters.

To avoid these problems, the environment used to start Apache should contain settings analogous to the following:

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


Consult the documentation for your operating system for the appropriate syntax and location to put these configuration items; /etc/apache2/envvars is
a common location on Unix platforms. Once you have added these statements to your environment, restart Apache.

也就是需要在:

/etc/apache2/envvars中添加:

export LANG='en_US.UTF-8'

export LC_ALL='en_US.UTF-8'

 添加完后执行apache2服务重新启动命令

sudo service apache2 restart

另外顺带把django上传带有中文名字文件报错UnicodeEncodeError的问题也给你们解决了,

但是上传的文件却出现了:[Errno 13] Permission denied 的错误,那是因为你没有给部署的网站项目目录以写权限

解决方法:执行下面的命令,把search替换成你的网站项目目录即可,

sudo chmod g+w -R search/
sudo chgrp www-data -R search/
注意:不要漏掉 -R , 这是递归赋予写权限,否则仍然报错,不要漏了/

 

找了几个月的各种资料,文档,就解决了这两个很重要的问题,天道酬勤!

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  django 中文乱码