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", "")
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
- [Django]-How to merge consecutive database migrations in django 1.9+?
- [Django]-How do I create multiple model instances with Django Rest Framework?
- [Django]-Django: using more than one database with inspectdb?
5π
You can use:
request.POST['title']
it will easily fetch the data with that title.
- [Django]-Sending an SMS to a Cellphone using Django
- [Django]-What is the difference between {% load staticfiles %} and {% load static %}
- [Django]-How to drop all tables from the database with manage.py CLI in Django?
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))
- [Django]-What's the idiomatic Python equivalent to Django's 'regroup' template tag?
- [Django]-Set Django IntegerField by choices=β¦ name
- [Django]-How do I use Django templates without the rest of Django?
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'} #}
- [Django]-Django models: mutual references between two classes and impossibility to use forward declaration in python
- [Django]-Whats the difference between using {{STATIC_URL}} and {% static %}
- [Django]-Why does Django's render() function need the "request" argument?