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

177πŸ‘

βœ…

Read about request objects that your views receive: https://docs.djangoproject.com/en/dev/ref/request-response/#httprequest-objects

Also your hidden field needs a reliable name and then a value:

<input type="hidden" name="title" value="{{ source.title }}">

Then in a view:

request.POST.get("title", "")
πŸ‘€jdi

15πŸ‘

If you need to do something on the front end you can respond to the onsubmit event of your form. If you are just posting to admin/start you can access post variables in your view through the request object. request.POST which is a dictionary of post variables

πŸ‘€dm03514

5πŸ‘

You can use:

request.POST['title']

it will easily fetch the data with that title.

πŸ‘€Akshat Sharma

4πŸ‘

For django forms you can do this;

form = UserLoginForm(data=request.POST) #getting the whole data from the user.
user = form.save() #saving the details obtained from the user.
username = user.cleaned_data.get("username") #where "username" in parenthesis is the name of the Charfield (the variale name i.e, username = forms.Charfield(max_length=64))
πŸ‘€Irfan wani

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 the POST request values in my_app1/views.py as shown below. *My answer explains how to get a POST request values’ list in Django and my answer explains how to get GET request values in Django:

# "my_app1/views.py"

from django.shortcuts import render

def test(request):

    print(request.POST['fruits']) # apple
    print(request.POST.get('meat')) # beef
    print(request.POST.get('fish')) # None
    print(request.POST.get('fish', "Doesn't exist")) # Doesn't exist
    print(request.POST.getlist('fruits')) # ['apple']
    print(request.POST.getlist('fish')) # []
    print(request.POST.getlist('fish', "Doesn't exist")) # Doesn't exist
    print(request.POST._getlist('meat')) # ['beef']
    print(request.POST._getlist('fish')) # []
    print(request.POST._getlist('fish', "Doesn't exist")) # Doesn't exist
    print(list(request.POST.keys())) # ['csrfmiddlewaretoken', 'fruits', 'meat']
    print(list(request.POST.values())) # ['b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS', 'apple', 'beef']
    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 the POST request values in test.html as shown below:

{# "test.html" #}

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

Leave a comment