sending content-encoding header in django

niklasfi picture niklasfi · Aug 8, 2010 · Viewed 8.9k times · Source

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

Answer

Davor Lucic picture Davor Lucic · Aug 8, 2010

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)