From f355d09cf6486a5b06a5d2bd7327f4f3a6e5c311 Mon Sep 17 00:00:00 2001
From: Chris Adams <chris@improbable.org>
Date: Tue, 25 Oct 2011 17:42:33 -0400
Subject: [PATCH] generic views: redirect_to should not interpolate query
string content (see #17111)
Now redirect_to will not include the query string value until after performing
string interpolation to avoid an exception with data containing valid Python
string formatting sequences (e.g. UTF-8 escaped values such as "hist%C3%B3ria").
---
django/views/generic/simple.py | 10 +++++++---
.../regressiontests/views/tests/generic/simple.py | 6 ++++++
2 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/django/views/generic/simple.py b/django/views/generic/simple.py
index 0e1e6b0..c3cf407 100644
a
|
b
|
def redirect_to(request, url, permanent=True, query_string=False, **kwargs):
|
49 | 49 | |
50 | 50 | """ |
51 | 51 | args = request.META.get('QUERY_STRING', '') |
52 | | if args and query_string and url is not None: |
53 | | url = "%s?%s" % (url, args) |
54 | 52 | |
55 | 53 | if url is not None: |
| 54 | if kwargs: |
| 55 | url = url % kwargs |
| 56 | |
| 57 | if args and query_string: |
| 58 | url = "%s?%s" % (url, args) |
| 59 | |
56 | 60 | klass = permanent and HttpResponsePermanentRedirect or HttpResponseRedirect |
57 | | return klass(url % kwargs) |
| 61 | return klass(url) |
58 | 62 | else: |
59 | 63 | logger.warning('Gone: %s' % request.path, |
60 | 64 | extra={ |
diff --git a/tests/regressiontests/views/tests/generic/simple.py b/tests/regressiontests/views/tests/generic/simple.py
index be54af9..ed51b3d 100644
a
|
b
|
class RedirectToTest(TestCase):
|
48 | 48 | self.assertEqual(response.status_code, 301) |
49 | 49 | self.assertEqual('http://testserver/simple/target/?param1=foo¶m2=bar', response['Location']) |
50 | 50 | |
| 51 | # Confirm that the contents of the query string are not subject to |
| 52 | # string interpolation: |
| 53 | response = self.client.get('/simple/redirect_to_query/?param1=foo¶m2=hist%C3%B3ria') |
| 54 | self.assertEqual(response.status_code, 301) |
| 55 | self.assertEqual('http://testserver/simple/target/?param1=foo¶m2=hist%C3%B3ria', response['Location']) |
| 56 | |
51 | 57 | def test_redirect_to_when_meta_contains_no_query_string(self): |
52 | 58 | "regression for #16705" |
53 | 59 | # we can't use self.client.get because it always sets QUERY_STRING |