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.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@9594 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
e9b90d9899
commit
b76d7c1dec
|
@ -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 @@ def direct_to_template(request, template, extra_context=None, mimetype=None, **k
|
|||
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 @@ def redirect_to(request, url, **kwargs):
|
|||
)
|
||||
|
||||
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()
|
||||
|
|
|
@ -129,14 +129,29 @@ If the given URL is ``None``, Django will return an ``HttpResponseGone`` (410).
|
|||
* ``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',
|
||||
|
|
|
@ -118,6 +118,12 @@ class ClientTest(TestCase):
|
|||
# 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/')
|
||||
|
|
|
@ -9,6 +9,7 @@ urlpatterns = patterns('',
|
|||
(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'^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),
|
||||
|
|
Loading…
Reference in New Issue