Ticket #9966: redirect_to_with_query_string2.diff
File redirect_to_with_query_string2.diff, 5.8 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..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 427ef91..5e8b442 100644
a b If the given URL is ``None``, Django will return an ``HttpResponseGone`` (410). 139 139 redirect will use status code 301. If ``False``, then the redirect will 140 140 use status code 302. By default, ``permanent`` is ``True``. 141 141 142 * ``query_string``: Whether to pass along the GET query string to 143 the new location. If ``True``, then the query string is appended 144 to the URL. If ``False``, then the query string is discarded. By 145 default, ``query_string`` is ``False``. 146 142 147 .. versionadded:: 1.1 143 148 The ``permanent`` keyword argument is new in Django 1.1. 144 149 150 145 151 **Example:** 146 152 147 153 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 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..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 6403ab3..d7779f7 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 (r'^simple/redirect_to_query/$', 'redirect_to', dict(url='/views/simple/target/', query_string=True)), 94 )