代码改变世界

Django学习笔记——RSS输出时的中文乱码问题

2012-04-17 10:23  贼寇在何方  阅读(1083)  评论(0编辑  收藏  举报

用Django的Rss201rev2Feed输出RSS内容时,得到的中文全部是乱码:

而实际上,我已经使用了utf-8编码输出了,可是Chrome还是把他识别成了GBK···

return HttpResponse(feed.writeString('utf-8'), minetype='application/rss+xml')

 

陈总说这个"utf-8"还应当写到Content-Type里面去,

各种探索过程我就不说了,minetype可以不设置,直接设定content_type即可:

return HttpResponse(feed.writeString('utf-8'), content_type='application/rss+xml; charset=utf-8')

 

顺便贴一下HttpResponse的初始化函数,为啥minetype中间没有下划线,而content_type中间得有个下划线呢···

def __init__(self, content='', mimetype=None, status=None,
        content_type=None):
    # _headers is a mapping of the lower-case name to the original case of
    # the header (required for working with legacy systems) and the header
    # value.  Both the name of the header and its value are ASCII strings.
    self._headers = {}
    self._charset = settings.DEFAULT_CHARSET
    if mimetype:
        content_type = mimetype     # For backwards compatibility
    if not content_type:
        content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
                self._charset)
    if not isinstance(content, basestring) and hasattr(content, '__iter__'):
        self._container = content
        self._is_string = False
    else:
        self._container = [content]
        self._is_string = True
    self.cookies = SimpleCookie()
    if status:
        self.status_code = status

    self['Content-Type'] = content_type