Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the querydict using loop

Tags:

python

django

I am creating a function that will display the values and the key of querydict. for example if I got a QueryDict on a GET Request.

<QueryDict: {u'val1':[u'aa'],u'val2':[u'ab'],u'val3':[u'ac'],u'val4':[u'ad'], ... u'valn':[u'an'] ...}>

my function is now just like this:

def displayQueryDicts(self, request):
    for x in request:
     print x # this will return the val1, val2, val3, val4, ..., valn
     print x .value() # I don't know how to print all the values

My question now is, how can I print the values just like this..

val1   aa
val2   ab
val3   ac
val4   ad
...    ...
valn   an
like image 558
gadss Avatar asked Dec 01 '22 17:12

gadss


2 Answers

According to the documentation:

HttpRequest.GET

A dictionary-like object containing all given HTTP GET parameters.

You can iterate over it like over a normal python dict:

for key, value in request.GET.items():
    print "%s %s" % (key, value)

Hope that helps.

like image 106
alecxe Avatar answered Dec 14 '22 17:12

alecxe


In python3 you would do

 for key, value in request.GET.items():
        print ("%s %s" % (key, value))
like image 28
Old_Mortality Avatar answered Dec 14 '22 15:12

Old_Mortality