代码之家  ›  专栏  ›  技术社区  ›  niklasfi Rahul Tripathi

在Django中发送内容编码头

  •  5
  • niklasfi Rahul Tripathi  · 技术社区  · 14 年前

    你好,我想有我的内容的纯文本版本。所以我有一个单独的模板。我打电话来 render_to_response 具有 mimetype="text/plain" 但我想告诉一个在HTTP响应中打开该页面的浏览器,内容是UTF-8编码的。我该怎么做(例如,我必须在其中添加什么内容) 呈现响应 )?

    1 回复  |  直到 14 年前
        1
  •  7
  •   Davor Lucic    14 年前

    只需将charset添加到mimetype,如下所示:

    mimetype="text/html; charset=utf-8"
    

    真正发生在幕后的是,mimetype在 render_to_response .

    httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)}
    return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
    

    并发送到 HttpResponse 把它设置为 content_type :

    if mimetype:
        content_type = mimetype     # For backwards compatibility
    if not content_type:
        content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
                    settings.DEFAULT_CHARSET)