Author: adrian
Date: 2008-12-07 22:53:34 -0600 (Sun, 07 Dec 2008)
New Revision: 9594
Modified:
django/trunk/django/views/generic/simple.py
django/trunk/docs/ref/generic-views.txt
django/trunk/tests/modeltests/test_client/models.py
django/trunk/tests/modeltests/test_client/urls.py
Log:
Added a 'permanent' argument the simple.redirect_to() generic view. It's True
by default, to match existing behavior. If set to False, the redirect will be a
302 instead of a 301. This is technically backwards-incompatible if you were
using the redirect_to generic view with a format-string key called 'permanent',
which is highly, highly unlikely.
Modified: django/trunk/django/views/generic/simple.py
===================================================================
--- django/trunk/django/views/generic/simple.py 2008-12-08 04:15:19 UTC (rev
9593)
+++ django/trunk/django/views/generic/simple.py 2008-12-08 04:53:34 UTC (rev
9594)
@@ -1,5 +1,5 @@
from django.template import loader, RequestContext
-from django.http import HttpResponse, HttpResponsePermanentRedirect,
HttpResponseGone
+from django.http import HttpResponse, HttpResponseRedirect,
HttpResponsePermanentRedirect, HttpResponseGone
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,12 @@
)
If the given url is ``None``, a HttpResponseGone (410) will be issued.
+
+ If the ``permanent`` argument is False, then the response will have a 302
+ HTTP status code. Otherwise, the status code will be 301.
"""
if url is not None:
- return HttpResponsePermanentRedirect(url % kwargs)
+ klass = permanent and HttpResponsePermanentRedirect or
HttpResponseRedirect
+ return klass(url % kwargs)
else:
return HttpResponseGone()
Modified: django/trunk/docs/ref/generic-views.txt
===================================================================
--- django/trunk/docs/ref/generic-views.txt 2008-12-08 04:15:19 UTC (rev
9593)
+++ django/trunk/docs/ref/generic-views.txt 2008-12-08 04:53:34 UTC (rev
9594)
@@ -129,14 +129,29 @@
* ``url``: The URL to redirect to, as a string. Or ``None`` to raise a 410
(Gone) HTTP error.
+**Optional arguments:**
+
+ * ``permanent``: Whether the redirect should be permanent. The only
+ difference here is the HTTP status code returned. If ``True``, then the
+ redirect will use status code 301. If ``False``, then the redirect will
+ use status code 302. By default, ``permanent`` is ``True``.
+
**Example:**
-This example redirects from ``/foo/<id>/`` to ``/bar/<id>/``::
+This example issues a permanent redirect (HTTP status code 301) from
+``/foo/<id>/`` to ``/bar/<id>/``::
urlpatterns = patterns('django.views.generic.simple',
('^foo/(?P<id>\d+)/$', 'redirect_to', {'url': '/bar/%(id)s/'}),
)
+This example issues a non-permanent redirect (HTTP status code 302) from
+``/foo/<id>/`` to ``/bar/<id>/``::
+
+ urlpatterns = patterns('django.views.generic.simple',
+ ('^foo/(?P<id>\d+)/$', 'redirect_to', {'url': '/bar/%(id)s/',
'permanent': False}),
+ )
+
This example returns a 410 HTTP error for requests to ``/bar/``::
urlpatterns = patterns('django.views.generic.simple',
Modified: django/trunk/tests/modeltests/test_client/models.py
===================================================================
--- django/trunk/tests/modeltests/test_client/models.py 2008-12-08 04:15:19 UTC
(rev 9593)
+++ django/trunk/tests/modeltests/test_client/models.py 2008-12-08 04:53:34 UTC
(rev 9594)
@@ -118,6 +118,12 @@
# Check that the response was a 301 (permanent redirect) with absolute
URI
self.assertRedirects(response,
'http://django.testserver/test_client/get_view/', status_code=301)
+ def test_temporary_redirect(self):
+ "GET a URL that does a non-permanent redirect"
+ response = self.client.get('/test_client/temporary_redirect_view/')
+ # Check that the response was a 302 (non-permanent redirect)
+ self.assertRedirects(response,
'http://testserver/test_client/get_view/', status_code=302)
+
def test_redirect_to_strange_location(self):
"GET a URL that redirects to a non-200 page"
response = self.client.get('/test_client/double_redirect_view/')
Modified: django/trunk/tests/modeltests/test_client/urls.py
===================================================================
--- django/trunk/tests/modeltests/test_client/urls.py 2008-12-08 04:15:19 UTC
(rev 9593)
+++ django/trunk/tests/modeltests/test_client/urls.py 2008-12-08 04:53:34 UTC
(rev 9594)
@@ -8,7 +8,8 @@
(r'^header_view/$', views.view_with_header),
(r'^raw_post_view/$', views.raw_post_view),
(r'^redirect_view/$', views.redirect_view),
- (r'^permanent_redirect_view/$', redirect_to, { 'url':
'/test_client/get_view/' }),
+ (r'^permanent_redirect_view/$', redirect_to, {'url':
'/test_client/get_view/'}),
+ (r'^temporary_redirect_view/$', redirect_to, {'url':
'/test_client/get_view/', 'permanent': False}),
(r'^double_redirect_view/$', views.double_redirect_view),
(r'^bad_view/$', views.bad_view),
(r'^form_view/$', views.form_view),
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---