Take your pick:
def my_view(request):
if request.method == 'POST':
print request.POST.get('my_field')
form = MyForm(request.POST)
print form['my_field'].value()
print form.data['my_field']
if form.is_valid():
print form.cleaned_data['my_field']
print form.instance.my_field
form.save()
print form.instance.id # now this one can access id/pk
Note: the field is accessed as soon as it's available.
I use django 1.7+ and python 2.7+, the solution above dose not work. And the input value in the form can be got use POST as below (use the same form above):
if form.is_valid():
data = request.POST.get('my_form_field_name')
print data
Hope this helps.
It is easy if you are using django version 3.1 and above
def login_view(request):
if(request.POST):
yourForm= YourForm(request.POST)
itemValue = yourForm['your_filed_name'].value()
# Check if you get the value
return HttpResponse(itemValue )
else:
return render(request, "base.html")
You can do this after you validate your data.
if myform.is_valid():
data = myform.cleaned_data
field = data['field']
Also, read the django docs. They are perfect.
To retrieve data from form which send post request you can do it like this
def login_view(request):
if(request.POST):
login_data = request.POST.dict()
username = login_data.get("username")
password = login_data.get("password")
user_type = login_data.get("user_type")
print(user_type, username, password)
return HttpResponse("This is a post request")
else:
return render(request, "base.html")
Source: Stackoverflow.com