首页 > Web开发 > 详细

Print to the console in django without UnicodeEncodeErrors

时间:2015-01-08 17:24:55      阅读:355      评论:0      收藏:0      [点我收藏+]

在线下环境的时候,正常运行,然后当线上环境,添加汉字报了如下的bug:

1 Traceback (most recent call last):  
2 File "views.py", line 107, in <module>
3     print kwargs
4 UnicodeEncodeError: ascii codec cant encode character u\xa0 in position 20: ordinal not in range(128)

 

猜测应该是线上环境经过uwsgi的时候出了问题,然后去搜uwsgi django UnicodeEncodeError

得到类似的答案:

http://stackoverflow.com/questions/24887929/locale-on-django-and-uwsgi-unicodeencodeerror
http://chase-seibert.github.io/blog/2014/01/12/python-unicode-console-output.html

 

看了两篇文章后找到两种解决方案:

1.在uwsgi文件中添加如下配置

env = PYTHONIOENCODING=UTF-8

2.在django的settings.py文件中加入

import sys
import codecs
sys.stdout = codecs.getwriter(utf8)(sys.stdout)
sys.stderr = codecs.getwriter(utf8)(sys.stderr)

以上两种方案经尝试后都解决了那个该bug。

 

总结:Your termianl can typically handle UTF8 characters just fine. The issue is actually that Python is just getting confused about what encoding the terminal accepts. 

 

Print to the console in django without UnicodeEncodeErrors

原文:http://www.cnblogs.com/smallc/p/4210907.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!