Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get all post data from a request in Django?

Is there any way get all the form names from a request in Django ?

<input type="text" name="getrow">

Html request

def demoform(request):
    if request.method=="POST"
       inputtxt=request.POST.get("getrow")
       return HttpResponse(...)

in the above I can get only from the name I know, what I need is to get all names of the django request and later parse it and get data.

like image 642
Chandra Pavan Avatar asked Apr 19 '16 13:04

Chandra Pavan


2 Answers

To display POST values in django you can do:

print(list(request.POST.items()))

You can also also use dict()

print(dict(request.POST.items()))
like image 72
CallMarl Avatar answered Oct 28 '22 06:10

CallMarl


Try use this:

def demoform(request):
    if request.method=="POST":
        inputtxt=request.POST['getrow']
        return HttpResponse(...)

But if you need print a dynamic POST data, for example send the slug of many products, (i made it 2 days ago "April 22, 2018") you need try this:

for key, value in request.POST.items():
    print('Key: %s' % (key) ) 
    # print(f'Key: {key}') in Python >= 3.7
    print('Value %s' % (value) )
    # print(f'Value: {value}') in Python >= 3.7
like image 27
Roberth Solís Avatar answered Oct 28 '22 05:10

Roberth Solís