[Solved]-How to get all POST request values in Django?

28👍

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

16👍

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()))

2👍

first of all the above answers of our friends have cleared everything about how to get all post data. Again I can explain for you that first check the request method, then you can print out on console as well.

if request.method == 'POST':
           print(request.POST)

BTW, request.POST returns a dictionary structured data, so if you know the requested data already then you can pass within the POST to retrieve.

if request.method == 'POST':
           print(request.POST['username'])

but, if you want to work on the requested I mean you want to filter out the desired data then just create a dictionary object then work on that.

post_data = dict()
if request.method == 'POST':
           post_data = request.POST
print(post_data['username'])

if you don’t know the key, then you can just filter out through retrieving all keys from dictionary.

for key, value in post_data.items():
         if key == 'username':
                   print(value)

that’s it, hope I answered you well.

0👍

For example, if you submit the POST request values in index.html as shown below:

{# "index.html" #}

<form action="{% url 'my_app1:test' %}" method="post">
  {% csrf_token %}
  <input type="text" name="fruits" value="apple" /></br>
  <input type="text" name="meat" value="beef" /></br>
  <input type="submit" />
</form>

Then, you can get all the POST request values in my_app1/views.py as shown below. *My answer explains how to get POST request values in Django:

# "my_app1/views.py"

from django.shortcuts import render

def test(request):

    print(list(request.POST.items())) # [('csrfmiddlewaretoken', 'b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS'), ('fruits', 'apple'), ('meat', 'beef')]
    print(list(request.POST.lists())) # [('csrfmiddlewaretoken', ['b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS']), ('fruits', ['apple']), ('meat', ['beef'])]
    print(request.POST.dict()) # {'csrfmiddlewaretoken': 'b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS', 'fruits': 'apple', 'meat': 'beef'}
    print(dict(request.POST)) # {'csrfmiddlewaretoken': ['b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS'], 'fruits': ['apple'], 'meat': ['beef']}

    return render(request, 'test.html')

Then, you can get all the POST request values in test.html as shown below:

{# "test.html" #}

{{ request.POST.dict }} {# {'csrfmiddlewaretoken': 'Vzjk89LPweM4loDWTb9gFNHlRQNJRMNwzQWsiUaWNhgBOr8aLfZyPjHobgqFJimk', 'fruits': 'apple', 'meat': 'beef'} #}

Leave a comment