Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can you loop through the HttpRequest post variables in Django?

I have

for k,v in request.POST:
     print k,v

which is not working properly.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

request.POST is a dictionary-like object containing all given HTTP POST parameters.

When you loop through request.POST, you only get the keys.

for key in request.POST:
    print(key)
    value = request.POST[key]
    print(value)

To retrieve the keys and values together, use the items method.

for key, value in request.POST.items():
    print(key, value)

Note that request.POST can contain multiple items for each key. If you are expecting multiple items for each key, you can use lists, which returns all values as a list.

for key, values in request.POST.lists():
    print(key, values)

For more information see the Django docs for QueryDict.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...