cannot urlencode() after storing QueryDict in session
Question: I tried to post this to django-users group (
<http://groups.google.com/group/django-
users/browse_thread/thread/8572d7f4075cfe0e> ) but got no responses. Maybe
here I will get more help.

I store `request.GET` in session:

    
    
    request.session['query_string'] = request.GET
    

then I retrieve the value in another page and try to urlencode the QueryDict:

    
    
    context['query_string'] = request.session['query_string'].urlencode()
    

in my context I get the python's string representation of the QueryDict object
instead of the expected `key0=value0&key1=value1&...` string.

If, instead of QueryDict, I store the urlencoded string in the session,
everything works of course:

    
    
    request.session['query_string'] = request.GET.urlencode()
    

is it a bug?


Answer: This is not a bug. If you take a peek at the `QueryDict` definition (see
[https://github.com/django/django/blob/master/django/http/**init**.py](https://github.com/django/django/blob/master/django/http/__init__.py)),
it says explicitly that it's immutable unless you create a copy of it.

To demonstrate this, here's what I have in my Python shell,

    
    
    >>> from django.http import QueryDict
    >>> q1 = QueryDict('', mutable=False)
    >>> q2 = QueryDict('', mutable=True)
    >>> q1['next'] = '/a&b/'
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File "/Users/kenny/Desktop/Kreybits/locker/python/lib/python2.7/site-packages/django/http/__init__.py", line 357, in __setitem__
        self._assert_mutable()
      File "/Users/kenny/Desktop/Kreybits/locker/python/lib/python2.7/site-packages/django/http/__init__.py", line 354, in _assert_mutable
        raise AttributeError("This QueryDict instance is immutable")
    AttributeError: This QueryDict instance is immutable
    >>> q2['next'] = '/a&b/'
    >>> q2.urlencode()
    'next=%2Fa%26b%2F'
    

The `mutable` argument is set to False by default, and since
`request.session['query_string'] = request.GET` initialized it to an empty
QueryDict to begin with, calling `urlencode()` only returns you an empty str
while the `request.session['query_string'] = request.GET.urlencode()` works
because you're working with a QueryDict that has been initialized with the
appropriate key/values.

