Code

Opened 6 years ago

Closed 5 years ago

Last modified 5 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: UI/UX:

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@…> 6 years ago.
Patch

Download all attachments as: .zip

Change History (5)

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

Patch

comment:1 Changed 6 years ago by toke

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

comment:2 Changed 5 years ago by FilipeCorreia

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

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

  • Resolution set to fixed
  • Status changed from new to closed

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 5 years ago by kmtracey

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.