Django Generic Class Based Views - Change Content-Type

252 views
Skip to first unread message

Ross C

unread,
May 30, 2012, 2:59:21 AM5/30/12
to django...@googlegroups.com
Hi there,

Firstly I would like to say how awesome Django is and as a long time user I am stoked at the 1.4 changes and coming 1.5 :)

I have a minor question and looking through the source (although maybe i missed it) I cant seem to see how to change the HttpResponse content_type of a Generic class based view.

Anyone point me in the right direction or know the answer outright?

all the best
Ross

Iván Raskovsky

unread,
May 30, 2012, 10:10:33 AM5/30/12
to django...@googlegroups.com
Hi Ross,

On Wed, May 30, 2012 at 3:59 AM, Ross C <sendro...@gmail.com> wrote:
> I have a minor question and looking through the source (although maybe i
> missed it) I cant seem to see how to change the HttpResponse content_type of
> a Generic class based view.
>
> Anyone point me in the right direction or know the answer outright?

First of all check out the TemplateResponse[0] and then the
TemplateResponseMixin[1] specifically render_to_response() where you
can subclass and call super() with new kwargs. In the case you are
looking for adding 'content_type' to the kwargs, to change it to
whatever you want.

Simply add the following to your view:

def render_to_response(self, context, **response_kwargs):
response_kwargs.update({'content_type': 'yyy/xxx'})
return super(MyView, self).render_to_response(context,
**response_kwargs)

Regards,
Iván

[0] https://docs.djangoproject.com/en/dev/ref/template-response/
[1] https://docs.djangoproject.com/en/dev/ref/class-based-views/#templateresponsemixin
Reply all
Reply to author
Forward
0 new messages