sending content-encoding header in django
Asked Answered
L

1

7

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

Lithomarge answered 8/8, 2010 at 12:27 Comment(0)
L
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)
Lockup answered 8/8, 2010 at 13:25 Comment(1)
In new versions of Django the parameter is called content_type insted of mimetype.Hackman

© 2022 - 2024 — McMap. All rights reserved.