Skip to content
Browse files

Fixed #22799 -- Made GET and POST on HttpRequest QueryDicts, and FILE…

…S a MultiValueDict.

Previously, GET, POST, and FILES on an HttpRequest were created in
the __init__ method as dictionaries. This was not something you would
usually notice causing trouble in production as you'd only see a
WSGIRequest, but in testing using the test client, calling .getlist
on GET, POST, or FILES for a request with no get/post data resulted in
an AttributeError.

Changed GET and POST on an HttpRequest object to be mutable
QueryDicts (mutable because the Django tests, and probably many
third party tests, were expecting it).
  • Loading branch information...
1 parent d68987a commit fd4ccd045cf8edf0bf10ee0c576c7b7c5819170c @duncanparkes duncanparkes committed with timgraham
Showing with 19 additions and 1 deletion.
  1. +6 −1 django/http/request.py
  2. +6 −0 docs/releases/1.8.txt
  3. +7 −0 tests/requests/tests.py
View
7 django/http/request.py
@@ -49,7 +49,12 @@ def __init__(self):
# Any variable assignment made here should also happen in
# `WSGIRequest.__init__()`.
- self.GET, self.POST, self.COOKIES, self.META, self.FILES = {}, {}, {}, {}, {}
+ self.GET = QueryDict(mutable=True)
+ self.POST = QueryDict(mutable=True)
+ self.COOKIES = {}
+ self.META = {}
+ self.FILES = MultiValueDict()
+
self.path = ''
self.path_info = ''
self.method = None
View
6 docs/releases/1.8.txt
@@ -220,6 +220,12 @@ Requests and Responses
instantiated with ``QueryDict()`` instead of ``QueryDict(None)`` or
``QueryDict('')``.
+* The ``GET`` and ``POST`` attributes of an :class:`~django.http.HttpRequest`
+ object are now :class:`~django.http.QueryDict`\s rather than dictionaries,
+ and the ``FILES`` attribute is now a ``MultiValueDict``.
+ This brings this class into line with the documentation and with
+ ``WSGIRequest``.
+
Tests
^^^^^
View
7 tests/requests/tests.py
@@ -27,6 +27,13 @@ def test_httprequest(self):
self.assertEqual(list(request.COOKIES.keys()), [])
self.assertEqual(list(request.META.keys()), [])
+ # .GET and .POST should be QueryDicts
+ self.assertEqual(request.GET.urlencode(), '')
+ self.assertEqual(request.POST.urlencode(), '')
+
+ # and FILES should be MultiValueDict
+ self.assertEqual(request.FILES.getlist('foo'), [])
+
def test_httprequest_repr(self):
request = HttpRequest()
request.path = '/somepath/'

0 comments on commit fd4ccd0

Please sign in to comment.
Something went wrong with that request. Please try again.