发送在Django content-encoding头
-
26-09-2019 - |
题
您好我想将自己的内容提供的明文版本。所以我有一个单独的模板。我打电话跟render_to_response
mimetype="text/plain"
但我想告诉浏览器中打开该页面的HTTP响应内容是UTF-8编码。我如何做到这一点(例如我有什么要添加到render_to_response
)?
解决方案
只需添加字符集到的mimetype这样的:
mimetype="text/html; charset=utf-8"
真正发生的场景背后是什么MIME类型是采取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)
不隶属于 StackOverflow