Opened 16 years ago

Closed 16 years ago

Last modified 16 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: dev
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

Description

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.

Index: simple.py
===================================================================
--- simple.py	(revision 8135)
+++ simple.py	(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)
     else:
         return HttpResponseGone()

Attachments (1)

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

Download all attachments as: .zip

Change History (5)

by David Sauve <dnsauve@…>, 16 years ago

Attachment: simple.patch added

Patch

comment:1 by Thomas Kerpe, 16 years ago

Triage Stage: UnreviewedDesign decision needed

comment:2 by FilipeCorreia, 16 years ago

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

comment:3 by David Sauve <dnsauve@…>, 16 years ago

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 by Karen Tracey, 16 years ago

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