Ticket #9966: redirect_to_with_query_string.diff

File redirect_to_with_query_string.diff, 6.5 KB (added by steingrd, 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  
    1919
    2020def redirect_to(request, url, permanent=True, **kwargs):
    2121    """
    22     Redirect to a given URL.
     22    Redirect to a given URL, passing through the query string.
    2323
    2424    The given url may contain dict-style string formatting, which will be
    2525    interpolated against the params in the URL.  For example, to redirect from
    def redirect_to(request, url, permanent=True, **kwargs):  
    3434    If the ``permanent`` argument is False, then the response will have a 302
    3535    HTTP status code. Otherwise, the status code will be 301.
    3636    """
     37    query_string = request.META["QUERY_STRING"]
     38    if query_string and url is not None:
     39        url = "%s?%s" % (url, query_string)
     40
    3741    if url is not None:
    3842        klass = permanent and HttpResponsePermanentRedirect or HttpResponseRedirect
    3943        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):  
    204204        response = self.client.head('/test_client_regress/redirects/',
    205205            {'nothing': 'to_send'}, follow=True)
    206206        self.assertRedirects(response,
    207             '/test_client_regress/no_template_view/', 301, 200)
     207            '/test_client_regress/no_template_view/?nothing=to_send', 301, 200)
    208208        self.assertEquals(len(response.redirect_chain), 3)
    209209
    210210    def test_redirect_chain_options(self):
    class AssertRedirectsTests(TestCase):  
    212212        response = self.client.options('/test_client_regress/redirects/',
    213213            {'nothing': 'to_send'}, follow=True)
    214214        self.assertRedirects(response,
    215             '/test_client_regress/no_template_view/', 301, 200)
     215            '/test_client_regress/no_template_view/?nothing=to_send', 301, 200)
    216216        self.assertEquals(len(response.redirect_chain), 3)
    217217
    218218    def test_redirect_chain_put(self):
    class AssertRedirectsTests(TestCase):  
    220220        response = self.client.put('/test_client_regress/redirects/',
    221221            {'nothing': 'to_send'}, follow=True)
    222222        self.assertRedirects(response,
    223             '/test_client_regress/no_template_view/', 301, 200)
     223            '/test_client_regress/no_template_view/?nothing=to_send', 301, 200)
    224224        self.assertEquals(len(response.redirect_chain), 3)
    225225
    226226    def test_redirect_chain_delete(self):
    class AssertRedirectsTests(TestCase):  
    228228        response = self.client.delete('/test_client_regress/redirects/',
    229229            {'nothing': 'to_send'}, follow=True)
    230230        self.assertRedirects(response,
    231             '/test_client_regress/no_template_view/', 301, 200)
     231            '/test_client_regress/no_template_view/?nothing=to_send', 301, 200)
    232232        self.assertEquals(len(response.redirect_chain), 3)
    233233
    234234    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 *  
    33from static import *
    44from generic.date_based import *
    55from generic.create_update import *
     6from 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
     3from django.test import TestCase
     4
     5class 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&param2=bar')
     32        self.assertEqual(response.status_code, 301)
     33        self.assertEqual('http://testserver/views/simple/target/?param1=foo&param2=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',  
    8282    (r'^create_update/no_url/update/article/(?P<slug>[-\w]+)/$',
    8383        'update_object', dict(slug_field='slug', model=UrlArticle)),
    8484)
     85
     86# simple generic views.
     87
     88urlpatterns += 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)
Back to Top