Ticket #9966: redirect_to_with_query_string_r11688.diff
File redirect_to_with_query_string_r11688.diff, 6.0 KB (added by , 15 years ago) |
---|
-
django/views/generic/simple.py
diff --git a/django/views/generic/simple.py b/django/views/generic/simple.py index 3b5309d..435cd76 100644
a b def direct_to_template(request, template, extra_context=None, mimetype=None, **k 17 17 t = loader.get_template(template) 18 18 return HttpResponse(t.render(c), mimetype=mimetype) 19 19 20 def redirect_to(request, url, permanent=True, **kwargs):20 def redirect_to(request, url, permanent=True, query_string=False, **kwargs): 21 21 """ 22 22 Redirect to a given URL. 23 23 … … def redirect_to(request, url, permanent=True, **kwargs): 33 33 34 34 If the ``permanent`` argument is False, then the response will have a 302 35 35 HTTP status code. Otherwise, the status code will be 301. 36 37 If the ``query_string`` argument is True, then the GET query string 38 from the request is appended to the URL. 39 36 40 """ 41 args = request.META["QUERY_STRING"] 42 if args and query_string and url is not None: 43 url = "%s?%s" % (url, args) 44 37 45 if url is not None: 38 46 klass = permanent and HttpResponsePermanentRedirect or HttpResponseRedirect 39 47 return klass(url % kwargs) -
docs/ref/generic-views.txt
diff --git a/docs/ref/generic-views.txt b/docs/ref/generic-views.txt index 4752a70..625bf13 100644
a b If the given URL is ``None``, Django will return an ``HttpResponseGone`` (410). 90 90 redirect will use status code 301. If ``False``, then the redirect will 91 91 use status code 302. By default, ``permanent`` is ``True``. 92 92 93 * ``query_string``: Whether to pass along the GET query string to 94 the new location. If ``True``, then the query string is appended 95 to the URL. If ``False``, then the query string is discarded. By 96 default, ``query_string`` is ``False``. 97 93 98 .. versionadded:: 1.1 94 99 The ``permanent`` keyword argument is new in Django 1.1. 95 100 101 96 102 **Example:** 97 103 98 104 This example issues a permanent redirect (HTTP status code 301) from -
tests/regressiontests/views/tests/__init__.py
diff --git a/tests/regressiontests/views/tests/__init__.py b/tests/regressiontests/views/tests/__init__.py index 697968e..edd533e 100644
a b from debug import * 2 2 from defaults import * 3 3 from generic.create_update import * 4 4 from generic.date_based import * 5 from generic.simple import * 5 6 from i18n import * 6 7 from specials import * 7 8 from static import * -
new file tests/regressiontests/views/tests/generic/simple.py
diff --git a/tests/regressiontests/views/tests/generic/simple.py b/tests/regressiontests/views/tests/generic/simple.py new file mode 100644 index 0000000..f94b3da
- + 1 # coding: utf-8 2 3 from django.test import TestCase 4 5 class RedirectToTest(TestCase): 6 def test_redirect_to_returns_permanent_redirect(self): 7 "simple.redirect_to returns a permanent redirect (301) by default" 8 response = self.client.get('/views/simple/redirect_to/') 9 self.assertEqual(response.status_code, 301) 10 self.assertEqual('http://testserver/views/simple/target/', response['Location']) 11 12 def test_redirect_to_can_return_a_temporary_redirect(self): 13 "simple.redirect_to returns a temporary redirect (302) when explicitely asked to" 14 response = self.client.get('/views/simple/redirect_to_temp/') 15 self.assertEqual(response.status_code, 302) 16 self.assertEqual('http://testserver/views/simple/target/', response['Location']) 17 18 def test_redirect_to_on_empty_url_returns_gone(self): 19 "simple.redirect_to returns resource gone (410) when given a None url" 20 response = self.client.get('/views/simple/redirect_to_none/') 21 self.assertEqual(response.status_code, 410) 22 23 def test_redirect_to_allows_formatted_url_string(self): 24 "simple.redirect_to uses string interpolation on target url for keyword args" 25 response = self.client.get('/views/simple/redirect_to_arg/42/') 26 self.assertEqual(response.status_code, 301) 27 self.assertEqual('http://testserver/views/simple/target_arg/42/', response['Location']) 28 29 def test_redirect_to_allows_query_string_to_be_passed(self): 30 "simple.redirect_to configured with query_string=True passes on any query string" 31 # the default is to not forward the query string 32 response = self.client.get('/views/simple/redirect_to/?param1=foo¶m2=bar') 33 self.assertEqual(response.status_code, 301) 34 self.assertEqual('http://testserver/views/simple/target/', response['Location']) 35 # views configured with query_string=True however passes the query string along 36 response = self.client.get('/views/simple/redirect_to_query/?param1=foo¶m2=bar') 37 self.assertEqual(response.status_code, 301) 38 self.assertEqual('http://testserver/views/simple/target/?param1=foo¶m2=bar', response['Location']) -
tests/regressiontests/views/urls.py
diff --git a/tests/regressiontests/views/urls.py b/tests/regressiontests/views/urls.py index eee1166..e2349a0 100644
a b urlpatterns += patterns('django.views.generic.date_based', 65 65 ) 66 66 67 67 # crud generic views. 68 69 68 urlpatterns += patterns('django.views.generic.create_update', 70 69 (r'^create_update/member/create/article/$', 'create_object', 71 70 dict(login_required=True, model=Article)), … … urlpatterns += patterns('django.views.generic.create_update', 97 96 urlpatterns += patterns('', 98 97 (r'^raises/$', views.raises) 99 98 ) 99 100 # simple generic views. 101 urlpatterns += patterns('django.views.generic.simple', 102 (r'^simple/redirect_to/$', 'redirect_to', dict(url='/views/simple/target/')), 103 (r'^simple/redirect_to_temp/$', 'redirect_to', dict(url='/views/simple/target/', permanent=False)), 104 (r'^simple/redirect_to_none/$', 'redirect_to', dict(url=None)), 105 (r'^simple/redirect_to_arg/(?P<id>\d+)/$', 'redirect_to', dict(url='/views/simple/target_arg/%(id)s/')), 106 (r'^simple/redirect_to_query/$', 'redirect_to', dict(url='/views/simple/target/', query_string=True)), 107 )