您好我想要提供我内容的纯文本版本。所以我有一个单独的模板。我用render_to_response
呼叫mimetype="text/plain"
,但我想告诉浏览器在http响应中打开该页面,内容是utf-8编码的。我该怎么做(例如我必须添加到render_to_response
)?
答案 0 :(得分:7)
只需将字符集添加到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)