开发者

sending content-encoding header in django

开发者 https://www.devze.com 2023-01-10 12:42 出处:网络
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 open

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 pag开发者_Python百科e 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)?


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消