部署环境:
ubuntu server 16.04
apache2
python 3.6.2 (使用conda虚拟环境)
django 1.11
我在django的views.py代码里有这么一句:
filepath = settings.PART_MESSAGE_FILEPATH + '/' + file.name
with open(filepath, 'wb+') as new_file:
for chunk in file.chunks():
new_file.write(chunk)
当这个文件的名字包含中文时,就会报错,在apache的errorlog上会留下这样的记录.
文件名不包含中文的时候,就一切正常
[Wed Dec 06 13:02:58.474614 2017] [wsgi:error] [pid 2648:tid 139832153040640] WARNING 2017-12-06 13:02:58,474 /*****/views.py views.py views set_compomsg_file 183: huhai\xe4\$
[Wed Dec 06 13:25:43.729044 2017] [wsgi:error] [pid 3284:tid 139689529792256] Internal Server Error: /temrule/updatefile/
[Wed Dec 06 13:25:43.729067 2017] [wsgi:error] [pid 3284:tid 139689529792256] Traceback (most recent call last):
[Wed Dec 06 13:25:43.729070 2017] [wsgi:error] [pid 3284:tid 139689529792256] File "/data2/serverend/comengine/templaterule/views.py", line 154, in set_compomsg_file
[Wed Dec 06 13:25:43.729072 2017] [wsgi:error] [pid 3284:tid 139689529792256] with open(filepath, 'wb+') as new_file:
[Wed Dec 06 13:25:43.729073 2017] [wsgi:error] [pid 3284:tid 139689529792256] UnicodeEncodeError: 'ascii' codec can't encode characters in position 57-60: ordinal not in range(128)
我的conf文件
<VirtualHost *:7090>
ServerName localhost:7090
ServerAdmin sdfs@dsds.com
WSGIDaemonProcess comengine python-path=/home/yangtz/anaconda3/envs/compoengine/lib/python3.6/site-packages
WSGIProcessGroup comengine
WSGIScriptAlias / /data2/serverend/comengine/comengine/wsgi.py
#Directory里是项目wsgi文件的父目录,形如:/项目所处路径/dpsplat/visualplat
<Directory /data2/serverend/comengine/comengine>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error-comengine.log
CustomLog ${APACHE_LOG_DIR}/access-comengine.log combined
</VirtualHost>
我的环境明明是python3.6啊,怎么会出现UnicodeEncodeError呢?在本地runserver的时候就不会有这种事,部署到服务器 apache上就会这样,这是怎么回事?该怎么办呢?
问题已经解决了。
http://blog.csdn.net/happen23...