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
)?
Asked
Active
Viewed 9,463 times
7

Davor Lucic
- 28,970
- 8
- 66
- 76

niklasfi
- 15,245
- 7
- 40
- 54
1 Answers
10
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)

Davor Lucic
- 28,970
- 8
- 66
- 76
-
1In new versions of Django the parameter is called content_type insted of mimetype. – Emil Stenström Jan 14 '16 at 09:35