[Django]-How to obtain all values of a multi-valued key from Django's request.GET QueryDict

16👍

This should work:

mstring = []
for key in request.GET.iterkeys():  # "for key in request.GET" works too.
    # Add filtering logic here.
    valuelist = request.GET.getlist(key)
    mstring.extend(['%s=%s' % (key, val) for val in valuelist])
print '&'.join(mstring)

26👍

request.META['QUERY_STRING']

will give the complete query string

or if you want to get the list of values for a given key
ex: list of values for status then

request.GET.getlist('status')
👤Ashok

8👍

I believe QueryDict.urlencode achieves your desired outcome if all you want to do is print out the QueryDict then just

print request.GET.urlencode()

should do the trick. Let me know if you were trying to do something else and I’ll try to help!

👤David

8👍

request.GET.getlist('status')

4👍

It’s easy!
Just print(dict(request.GET))

2👍

To obtain all the values from one. It’s better to use getlist("key", default=list). All the values from the key is stored in a list.

request.POST.getlist('key',default=list)
👤JekR

1👍

you can cast the querydict into a dictionary

map(int,dict(request.GET)["status"])

0👍

There is a useful function in django http utils you can use:

>>> from django.utils.http import urlencode
>>> print(urlencode({"tag": [1, 2, 3], "sentence":2}, doseq=True))

'tag=1&tag=2&tag=3&sentence=2'

Leave a comment