How to get POST request values in Django?
Question:
I have the following django template (http://IP/admin/start/ is assigned to a hypothetical view called view):
{% for source in sources %}
<tr>
<td>{{ source }}</td>
<td>
<form action="/admin/start/" method="post">
{% csrf_token %}
<input type="hidden" name="{{ source.title }}">
<input type="submit" value="Start" class="btn btn-primary">
</form>
</td>
</tr>
{% endfor %}
sources
is the objects.all()
of a Django model being referenced in the view. Whenever a “Start” submit input is clicked, I want the “start” view to use the {{ source.title}}
data in a function before returning a rendered page. How do I gather information POSTed (in this case, in the hidden input) into Python variables?
Answers:
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
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", "")
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))
You can use:
request.POST['title']
it will easily fetch the data with that title.
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'} #}
I have the following django template (http://IP/admin/start/ is assigned to a hypothetical view called view):
{% for source in sources %}
<tr>
<td>{{ source }}</td>
<td>
<form action="/admin/start/" method="post">
{% csrf_token %}
<input type="hidden" name="{{ source.title }}">
<input type="submit" value="Start" class="btn btn-primary">
</form>
</td>
</tr>
{% endfor %}
sources
is the objects.all()
of a Django model being referenced in the view. Whenever a “Start” submit input is clicked, I want the “start” view to use the {{ source.title}}
data in a function before returning a rendered page. How do I gather information POSTed (in this case, in the hidden input) into Python variables?
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
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", "")
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))
You can use:
request.POST['title']
it will easily fetch the data with that title.
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'} #}