Ticket #9966: redirect_to_with_query_string.diff
File redirect_to_with_query_string.diff, 6.5 KB (added by , 16 years ago) |
---|
-
django/views/generic/simple.py
diff --git a/django/views/generic/simple.py b/django/views/generic/simple.py index 3b5309d..4c4ad6a 100644
a b def direct_to_template(request, template, extra_context=None, mimetype=None, **k 19 19 20 20 def redirect_to(request, url, permanent=True, **kwargs): 21 21 """ 22 Redirect to a given URL .22 Redirect to a given URL, passing through the query string. 23 23 24 24 The given url may contain dict-style string formatting, which will be 25 25 interpolated against the params in the URL. For example, to redirect from … … def redirect_to(request, url, permanent=True, **kwargs): 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 36 """ 37 query_string = request.META["QUERY_STRING"] 38 if query_string and url is not None: 39 url = "%s?%s" % (url, query_string) 40 37 41 if url is not None: 38 42 klass = permanent and HttpResponsePermanentRedirect or HttpResponseRedirect 39 43 return klass(url % kwargs) -
tests/regressiontests/test_client_regress/models.py
diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py index 5d65092..adca836 100644
a b class AssertRedirectsTests(TestCase): 204 204 response = self.client.head('/test_client_regress/redirects/', 205 205 {'nothing': 'to_send'}, follow=True) 206 206 self.assertRedirects(response, 207 '/test_client_regress/no_template_view/ ', 301, 200)207 '/test_client_regress/no_template_view/?nothing=to_send', 301, 200) 208 208 self.assertEquals(len(response.redirect_chain), 3) 209 209 210 210 def test_redirect_chain_options(self): … … class AssertRedirectsTests(TestCase): 212 212 response = self.client.options('/test_client_regress/redirects/', 213 213 {'nothing': 'to_send'}, follow=True) 214 214 self.assertRedirects(response, 215 '/test_client_regress/no_template_view/ ', 301, 200)215 '/test_client_regress/no_template_view/?nothing=to_send', 301, 200) 216 216 self.assertEquals(len(response.redirect_chain), 3) 217 217 218 218 def test_redirect_chain_put(self): … … class AssertRedirectsTests(TestCase): 220 220 response = self.client.put('/test_client_regress/redirects/', 221 221 {'nothing': 'to_send'}, follow=True) 222 222 self.assertRedirects(response, 223 '/test_client_regress/no_template_view/ ', 301, 200)223 '/test_client_regress/no_template_view/?nothing=to_send', 301, 200) 224 224 self.assertEquals(len(response.redirect_chain), 3) 225 225 226 226 def test_redirect_chain_delete(self): … … class AssertRedirectsTests(TestCase): 228 228 response = self.client.delete('/test_client_regress/redirects/', 229 229 {'nothing': 'to_send'}, follow=True) 230 230 self.assertRedirects(response, 231 '/test_client_regress/no_template_view/ ', 301, 200)231 '/test_client_regress/no_template_view/?nothing=to_send', 301, 200) 232 232 self.assertEquals(len(response.redirect_chain), 3) 233 233 234 234 def test_redirect_chain_on_non_redirect_page(self): -
tests/regressiontests/views/tests/__init__.py
diff --git a/tests/regressiontests/views/tests/__init__.py b/tests/regressiontests/views/tests/__init__.py index 9964cd5..12863ae 100644
a b from i18n import * 3 3 from static import * 4 4 from generic.date_based import * 5 5 from generic.create_update import * 6 from generic.simple import * 7 -
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..3b3ff79
- + 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_passes_on_query_parameters(self): 30 "simple.redirect_to passes on any GET parameters to the target url" 31 response = self.client.get('/views/simple/redirect_to/?param1=foo¶m2=bar') 32 self.assertEqual(response.status_code, 301) 33 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 6403ab3..246903b 100644
a b urlpatterns += patterns('django.views.generic.create_update', 82 82 (r'^create_update/no_url/update/article/(?P<slug>[-\w]+)/$', 83 83 'update_object', dict(slug_field='slug', model=UrlArticle)), 84 84 ) 85 86 # simple generic views. 87 88 urlpatterns += patterns('django.views.generic.simple', 89 (r'^simple/redirect_to/$', 'redirect_to', dict(url='/views/simple/target/')), 90 (r'^simple/redirect_to_temp/$', 'redirect_to', dict(url='/views/simple/target/', permanent=False)), 91 (r'^simple/redirect_to_none/$', 'redirect_to', dict(url=None)), 92 (r'^simple/redirect_to_arg/(?P<id>\d+)/$', 'redirect_to', dict(url='/views/simple/target_arg/%(id)s/')), 93 )