21

Last couple of days we were discussing at another question the best to manage randomness in a RESTful way; today I went to play a little bit with some ideas in Django only to find that there is no easy standard way of returning a 303 response (nor a 300 one, btw), that is, there doesn't seem to exist an HttpResponseSeeOther inside django.HTTP or in another place.

Do you know any means for achieving this?

Community
  • 1
  • 1
AticusFinch
  • 2,351
  • 3
  • 25
  • 32
  • Just for information: a proposition to add a `HttpResponseSeeOther` was closed with a wontfix in 2011. Source: https://code.djangoproject.com/ticket/13277 – DevOps Craftsman Oct 19 '22 at 10:02

2 Answers2

32

You could just override HttpResponse, like the other Responses do:

class HttpResponseSeeOther(HttpResponseRedirect):
    status_code = 303

return HttpResponseSeeOther('/other-url/')
gak
  • 32,061
  • 28
  • 119
  • 154
21

The generic HttpResponse object lets you specify any status code you want:

response = HttpResponse(content="", status=303)
response["Location"] = "http://example.com/redirect/here/"

If you need something re-usable then Gerald's answer is definitely valid; simply create your own HttpResponseSeeOther class. Django only provides these specific classes for a few of the most common status codes.

nezroy
  • 3,166
  • 22
  • 17