render_to_response and HTTP status codes other than 200

855 views
Skip to first unread message

chefsmart

unread,
Dec 4, 2009, 3:37:41 AM12/4/09
to Django users
According to the docs at http://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render-to-response
the render_to_response shortcut "Renders a given template with a given
context dictionary and returns an HttpResponse object with that
rendered text."

The HttpResponse object have a default status=200 (http://
docs.djangoproject.com/en/dev/ref/request-response/#id3)

How can I use the render_to_response to return HTTP status codes other
than 200?

Can render_to_response return HttpResponse subclasses like
HttpResponseRedirect, HttpResponseForbidden etc instead of the simple
HttpResponse?

Daniel Roseman

unread,
Dec 4, 2009, 5:51:25 AM12/4/09
to Django users
On Dec 4, 8:37 am, chefsmart <moran.cors...@gmail.com> wrote:
> According to the docs athttp://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render-to...
> the render_to_response shortcut "Renders a given template with a given
> context dictionary and returns an HttpResponse object with that
> rendered text."
>
> The HttpResponse object have a default status=200 (http://
> docs.djangoproject.com/en/dev/ref/request-response/#id3)
>
> How can I use the render_to_response to return HTTP status codes other
> than 200?
>
> Can render_to_response return HttpResponse subclasses like
> HttpResponseRedirect, HttpResponseForbidden etc instead of the simple
> HttpResponse?

You can, if you really want to. The only difference between
HttpResponse and HttpResponseForbidden is the status_code, so you can
just set the status_code attribute on the result of render_to_response
(which is an HttpResponse) to whatever you want.

However, I can't for the life of me think why you'd want to do this.
render_to_response is used, as the name implies, to render a template
and return it as an HttpResponse. In the case of 404 and 301 etc you
don't need a template, surely.
--
DR.

chefsmart

unread,
Dec 4, 2009, 6:34:13 AM12/4/09
to Django users
Yeah it's a twisted idea, but I'm going to use it for informative 403
405 pages.
Reply all
Reply to author
Forward
0 new messages