Author: mtredinnick Date: 2010-09-10 22:13:23 -0500 (Fri, 10 Sep 2010) New Revision: 13746
Added: django/trunk/tests/regressiontests/views/tests/generic/simple.py Modified: django/trunk/django/views/generic/simple.py django/trunk/docs/ref/generic-views.txt django/trunk/tests/regressiontests/views/tests/__init__.py django/trunk/tests/regressiontests/views/urls.py Log: Add option to redirect_to view to allow passing along the query string from the original request. Default is current behaviour, which is not to pass the query string (it often won't be appropriate to do so). Thanks to [email protected] for the patch and tests. Fixed #9966. Modified: django/trunk/django/views/generic/simple.py =================================================================== --- django/trunk/django/views/generic/simple.py 2010-09-11 03:04:28 UTC (rev 13745) +++ django/trunk/django/views/generic/simple.py 2010-09-11 03:13:23 UTC (rev 13746) @@ -17,7 +17,7 @@ t = loader.get_template(template) return HttpResponse(t.render(c), mimetype=mimetype) -def redirect_to(request, url, permanent=True, **kwargs): +def redirect_to(request, url, permanent=True, query_string=False, **kwargs): """ Redirect to a given URL. @@ -33,7 +33,15 @@ If the ``permanent`` argument is False, then the response will have a 302 HTTP status code. Otherwise, the status code will be 301. + + If the ``query_string`` argument is True, then the GET query string + from the request is appended to the URL. + """ + args = request.META["QUERY_STRING"] + if args and query_string and url is not None: + url = "%s?%s" % (url, args) + if url is not None: klass = permanent and HttpResponsePermanentRedirect or HttpResponseRedirect return klass(url % kwargs) Modified: django/trunk/docs/ref/generic-views.txt =================================================================== --- django/trunk/docs/ref/generic-views.txt 2010-09-11 03:04:28 UTC (rev 13745) +++ django/trunk/docs/ref/generic-views.txt 2010-09-11 03:13:23 UTC (rev 13746) @@ -88,9 +88,15 @@ redirect will use status code 301. If ``False``, then the redirect will use status code 302. By default, ``permanent`` is ``True``. + * ``query_string``: Whether to pass along the GET query string to + the new location. If ``True``, then the query string is appended + to the URL. If ``False``, then the query string is discarded. By + default, ``query_string`` is ``False``. + .. versionadded:: 1.1 The ``permanent`` keyword argument is new in Django 1.1. + **Example:** This example issues a permanent redirect (HTTP status code 301) from Modified: django/trunk/tests/regressiontests/views/tests/__init__.py =================================================================== --- django/trunk/tests/regressiontests/views/tests/__init__.py 2010-09-11 03:04:28 UTC (rev 13745) +++ django/trunk/tests/regressiontests/views/tests/__init__.py 2010-09-11 03:13:23 UTC (rev 13746) @@ -2,6 +2,7 @@ from defaults import * from generic.create_update import * from generic.date_based import * +from generic.simple import * from i18n import * from specials import * from static import * Added: django/trunk/tests/regressiontests/views/tests/generic/simple.py =================================================================== --- django/trunk/tests/regressiontests/views/tests/generic/simple.py (rev 0) +++ django/trunk/tests/regressiontests/views/tests/generic/simple.py 2010-09-11 03:13:23 UTC (rev 13746) @@ -0,0 +1,38 @@ +# coding: utf-8 + +from django.test import TestCase + +class RedirectToTest(TestCase): + def test_redirect_to_returns_permanent_redirect(self): + "simple.redirect_to returns a permanent redirect (301) by default" + response = self.client.get('/views/simple/redirect_to/') + self.assertEqual(response.status_code, 301) + self.assertEqual('http://testserver/views/simple/target/', response['Location']) + + def test_redirect_to_can_return_a_temporary_redirect(self): + "simple.redirect_to returns a temporary redirect (302) when explicitely asked to" + response = self.client.get('/views/simple/redirect_to_temp/') + self.assertEqual(response.status_code, 302) + self.assertEqual('http://testserver/views/simple/target/', response['Location']) + + def test_redirect_to_on_empty_url_returns_gone(self): + "simple.redirect_to returns resource gone (410) when given a None url" + response = self.client.get('/views/simple/redirect_to_none/') + self.assertEqual(response.status_code, 410) + + def test_redirect_to_allows_formatted_url_string(self): + "simple.redirect_to uses string interpolation on target url for keyword args" + response = self.client.get('/views/simple/redirect_to_arg/42/') + self.assertEqual(response.status_code, 301) + self.assertEqual('http://testserver/views/simple/target_arg/42/', response['Location']) + + def test_redirect_to_allows_query_string_to_be_passed(self): + "simple.redirect_to configured with query_string=True passes on any query string" + # the default is to not forward the query string + response = self.client.get('/views/simple/redirect_to/?param1=foo¶m2=bar') + self.assertEqual(response.status_code, 301) + self.assertEqual('http://testserver/views/simple/target/', response['Location']) + # views configured with query_string=True however passes the query string along + response = self.client.get('/views/simple/redirect_to_query/?param1=foo¶m2=bar') + self.assertEqual(response.status_code, 301) + self.assertEqual('http://testserver/views/simple/target/?param1=foo¶m2=bar', response['Location']) Modified: django/trunk/tests/regressiontests/views/urls.py =================================================================== --- django/trunk/tests/regressiontests/views/urls.py 2010-09-11 03:04:28 UTC (rev 13745) +++ django/trunk/tests/regressiontests/views/urls.py 2010-09-11 03:13:23 UTC (rev 13746) @@ -77,7 +77,6 @@ ) # crud generic views. - urlpatterns += patterns('django.views.generic.create_update', (r'^create_update/member/create/article/$', 'create_object', dict(login_required=True, model=Article)), @@ -123,3 +122,12 @@ url(r'view_exception/(?P<n>\d+)/$', 'view_exception', name='view_exception'), url(r'template_exception/(?P<n>\d+)/$', 'template_exception', name='template_exception'), ) + +# simple generic views. +urlpatterns += patterns('django.views.generic.simple', + (r'^simple/redirect_to/$', 'redirect_to', dict(url='/views/simple/target/')), + (r'^simple/redirect_to_temp/$', 'redirect_to', dict(url='/views/simple/target/', permanent=False)), + (r'^simple/redirect_to_none/$', 'redirect_to', dict(url=None)), + (r'^simple/redirect_to_arg/(?P<id>\d+)/$', 'redirect_to', dict(url='/views/simple/target_arg/%(id)s/')), + (r'^simple/redirect_to_query/$', 'redirect_to', dict(url='/views/simple/target/', query_string=True)), +) -- 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.
