【问题标题】:sending content-encoding header in django在 django 中发送内容编码标头
【发布时间】:2011-03-26 22:41:27
【问题描述】:

您好,我想要我的内容的纯文本版本。所以我有一个单独的模板。我用mimetype="text/plain" 调用render_to_response,但我想告诉浏览器在http-response 中打开该页面内容是utf-8 编码的。我该怎么做(例如,我必须添加什么到render_to_response)?

【问题讨论】:

    标签: django httpresponse content-encoding render-to-response


    【解决方案1】:

    只需像这样将字符集添加到 mimetype:

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

    幕后真正发生的是 mimetype 从 render_to_response 中的 kwargs 中取出。

    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)
    

    【讨论】:

    • 在新版本的 Django 中,该参数被称为 content_type insted of mimetype。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    • 2011-03-19
    • 2020-09-16
    • 1970-01-01
    • 2011-06-23
    相关资源
    最近更新 更多