winsmith winsmith - 1 year ago 378
Python Question

Django: Can I create a QueryDict from a dictionary?

Imagine that I have a dictionary in my Django application:

dict = {'a': 'one', 'b': 'two', }

Now I want to easily create an urlencoded list of GET parameters from this dictionary. Of course I could loop through the dictionary, urlencode keys and values and then concatenate the string by myself, but there must be an easier way. I would like to use a QueryDict instance. QueryDict is a subclass of dict, so it should be possible somehow.

qdict = QueryDict(dict) # this does not actually work
print qdict.urlencode()

How would I make the second to last line work?

Answer Source

How about?

dict = {'a': 'one', 'b': 'two', }
qdict = QueryDict('', mutable=True)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download