Django administration后台管理添加页面保持中文出错解决办法,'ascii' codec can't encode characters in position 0-4:

Django管理页面保存中文内容出现 ‘ascii’ codec can’t encode characters in position 0-4: 错误解决办法

如下在管理页面上添加信息的时候,字段内填写中文
Django administration后台管理添加页面保持中文出错解决办法,'ascii' codec can't encode characters in position 0-4:_第1张图片

提交后出现如下错误

UnicodeEncodeError at /admin/polls/question/add/
'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
Request Method: POST
Request URL:    http://192.168.1.50:8000/admin/polls/question/add/
Django Version: 1.9.6
Exception Type: UnicodeEncodeError
Exception Value:    
'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
Exception Location: /usr/local/lib/python2.7/dist-packages/django/utils/encoding.py in force_text, line 80
Python Executable:  /usr/bin/python
Python Version: 2.7.6
Python Path:    
['/home/saplingcode/site/mysite',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
Server time:    星期六, 21 五月 2016 16:10:48 +0800

解决办法:在admin.y添加下面三行代码即可

import sys;

reload(sys);
sys.setdefaultencoding("utf8")

Django administration后台管理添加页面保持中文出错解决办法,'ascii' codec can't encode characters in position 0-4:_第2张图片

修改后可以添加中文了
Django administration后台管理添加页面保持中文出错解决办法,'ascii' codec can't encode characters in position 0-4:_第3张图片

你可能感兴趣的:(Python)