Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#8022 closed (fixed)

Extend django.views.generic.simple.redirect_to to support permanent or temporary redirect codes

Reported by: David Sauve <dnsauve@…> Owned by: nobody
Component: Generic views Version: master
Severity: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


It's possible that this is completely inappropriate, but I've extended django.generic.simple.redirect_to with an extra, optional argument, 'permanent'.

By default, redirect_to returns a 301 return code, but if permanent=True is specified, it'll return a 302 instead.

Alternatively, a new generic view function, say temporary_redirect_to, could also do the same thing.

---	(revision 8135)
+++	(working copy)
@@ -1,5 +1,5 @@
 from django.template import loader, RequestContext
-from django.http import HttpResponse, HttpResponsePermanentRedirect, HttpResponseGone
+from django.http import HttpResponse, HttpResponsePermanentRedirect, HttpResponseGone,HttpResponseRedirect
 def direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs):
@@ -17,7 +17,7 @@
     t = loader.get_template(template)
     return HttpResponse(t.render(c), mimetype=mimetype)
-def redirect_to(request, url, **kwargs):
+def redirect_to(request, url, permanent=True, **kwargs):
     Redirect to a given URL.
@@ -30,8 +30,13 @@
     If the given url is ``None``, a HttpResponseGone (410) will be issued.
+    If permanent is True, the return code will be 301 - Moved Permanently,
+    otherwise, the return code will be 302 - Found.
     if url is not None:
-        return HttpResponsePermanentRedirect(url % kwargs)
+        if permanent:
+            return HttpResponsePermanentRedirect(url % kwargs)
+        else:
+            return HttpResponseRedirect(url % kwargs)
         return HttpResponseGone()

Attachments (1)

simple.patch (1.3 KB) - added by David Sauve <dnsauve@…> 10 years ago.

Download all attachments as: .zip

Change History (5)

Changed 10 years ago by David Sauve <dnsauve@…>

Attachment: simple.patch added


comment:1 Changed 10 years ago by Thomas Kerpe

Triage Stage: UnreviewedDesign decision needed

comment:2 Changed 10 years ago by FilipeCorreia

I think this is already part of django. See [9594].

comment:3 Changed 10 years ago by David Sauve <dnsauve@…>

Resolution: fixed
Status: newclosed

Hmm, looks like it is now, but wasn't when I opened this ticket in July. Still, done, is done, so I'll close the ticket.

comment:4 Changed 10 years ago by Karen Tracey

Yes, this was apparently added independently without realization that there was a ticket open for it. Happens sometimes.

Note: See TracTickets for help on using tickets.
Back to Top