Code

Ticket #19075: 19075-1.diff

File 19075-1.diff, 1.6 KB (added by claudep, 19 months ago)

Properly decode query string

Line 
1diff --git a/django/http/__init__.py b/django/http/__init__.py
2index ecb3912..3a9c57d 100644
3--- a/django/http/__init__.py
4+++ b/django/http/__init__.py
5@@ -392,7 +392,7 @@ class QueryDict(MultiValueDict):
6                                         encoding=encoding):
7                 self.appendlist(key, value)
8         else:
9-            for key, value in parse_qsl(query_string or '',
10+            for key, value in parse_qsl(force_bytes(query_string) if query_string else '',
11                                         keep_blank_values=True):
12                 self.appendlist(force_text(key, encoding, errors='replace'),
13                                 force_text(value, encoding, errors='replace'))
14diff --git a/tests/regressiontests/httpwrappers/tests.py b/tests/regressiontests/httpwrappers/tests.py
15index 4c6aed1..93f82ba 100644
16--- a/tests/regressiontests/httpwrappers/tests.py
17+++ b/tests/regressiontests/httpwrappers/tests.py
18@@ -95,6 +95,13 @@ class QueryDictTests(unittest.TestCase):
19         self.assertEqual(q.urlencode(), 'next=%2Ft%C3%ABst%26key%2F')
20         self.assertEqual(q.urlencode(safe='/'), 'next=/t%C3%ABst%26key/')
21 
22+    def test_querystring_decode(self):
23+        q = QueryDict(str('q=%D1%82%D0%B5%D1%81%D1%82'))
24+        self.assertEqual(q['q'], "тест")
25+        # With Python 2, unicode query string should also be properly decoded (#19075)
26+        q = QueryDict('q=%D1%82%D0%B5%D1%81%D1%82')
27+        self.assertEqual(q['q'], "тест")
28+
29     def test_mutable_copy(self):
30         """A copy of a QueryDict is mutable."""
31         q = QueryDict(str('')).copy()