views:

106

answers:

1

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!

+3  A: 

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

When you loop through request.POST, you only get the keys. To retrieve the keys and values together, use the iteritems method.

def my_view(request):
    # loop through keys
    for key in request.POST:
        value = request.POST['key']
    # loop through keys and values
    for key, value in request.POST.iteritems():
        # do something

For more information see the Django docs.

Alasdair
Better: `for key, value in request.POST.iteritems()`
Will McCutchen