Python- How to Retrieve Data from a Django Form

In this article, we show how to retrieve data from a Django form with Python.
There are 2 basic types of forms in Django that can perform the same exact thing.
One type of form is the Django form built with the Django form class.
The other type of form is just a form built hand-coded in plain HTML without using Django's form class.
Even though the two are very similar, they have subtle differences. We explain how to create and retrieve data from a form built with plain HTML at the following link: How to Create and Retrieve Data from a Form in Plain HTML in Django.
In this article, we focus on how to retrieve data from a Django form built with the Django form class.
Just to show quickly how to do before we go over a complete example. You would use the following code.
Basically to extract data from a form field of a form, all you have to do is use the form.is_valid() function along with the form.cleaned_data.get() function, passing in the name of the form field into this function as a parameter.
Now we'll go over a quick full example, so that you can see actual code.
So, we'll go right into it.
We're going to build a simple form, asking a user for his/her first name, last name, and email.
forms.py File
Below is the code in our forms.py file.
So, form's name is UserForm. 3 fields, first_name, last_name, and email.
Now let's go to our template file, where we combine this form with the template file, to have a complete form,
because we're still the HTML form tags, as well as the submit button.
Template File- index.html
So, below is the code in our template, named index.html.