Ticket #9966: redirect_to_with_query_string2.diff

File redirect_to_with_query_string2.diff, 5.8 KB (added by steingrd, 10 years ago)

adds a query_string optional keyword arg to redirect_to

  • 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 
    1717    t = loader.get_template(template)
    1818    return HttpResponse(t.render(c), mimetype=mimetype)
    1919
    20 def redirect_to(request, url, permanent=True, **kwargs):
     20def redirect_to(request, url, permanent=True, query_string=False, **kwargs):
    2121    """
    2222    Redirect to a given URL.
    2323
    def redirect_to(request, url, permanent=True, **kwargs): 
    3333
    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.
     36
     37    If the ``query_string`` argument is True, then the GET query string
     38    from the request is appended to the URL.
     39
    3640    """
     41    args = request.META["QUERY_STRING"]
     42    if args and query_string and url is not None:
     43        url = "%s?%s" % (url, args)
     44
    3745    if url is not None:
    3846        klass = permanent and HttpResponsePermanentRedirect or HttpResponseRedirect
    3947        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). 
    139139      redirect will use status code 301. If ``False``, then the redirect will
    140140      use status code 302. By default, ``permanent`` is ``True``.
    141141
     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
    142147.. versionadded:: 1.1
    143148    The ``permanent`` keyword argument is new in Django 1.1.
    144149
     150
    145151**Example:**
    146152
    147153This 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 * 
    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..f94b3da
    - +  
     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_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&param2=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&param2=bar')
     37        self.assertEqual(response.status_code, 301)
     38        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..d7779f7 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    (r'^simple/redirect_to_query/$', 'redirect_to', dict(url='/views/simple/target/', query_string=True)),
     94)
Back to Top