在 django 中发送内容编码标头

发布于 2024-09-13 12:36:49 字数 206 浏览 2 评论 0原文

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

Hello I want to have a plaintext version of my content available. So I have a separate template for that. I am calling render_to_response with mimetype="text/plain" but i want to tell a browser opening that page in the http-response that the content is utf-8 encoded. How do i do that (e.g. what do i have to add to render_to_response)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

草莓酥 2024-09-20 12:36:49

只需将字符集添加到 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)

Just add charset to mimetype like this:

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

What really happens behind scene is that mimetype is taken out of kwargs in render_to_response.

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

and sent to HttpResponse which sets it as 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文