Python- How to Retrieve Data from a Django Form



Python


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.



So we have the head tag, some meta, but the form is located in the body of the HTML file.

Since we want the data to remain on this page, we set the action attribute equal to "". We use the POST method, because we want the actual data in variables, not in the URL like the GET method.

The line, {% csrf_token %}, protects against cross site request forgery.

With the line, {{ form.as_p }}, we have our form rendered into the template file.

We then create the submit button and close the form.

The next block of code checks to see if the submit button was pressed. If it has, it outputs the data that the user has entered in. The variables you see in between the double curly braces that are those created in the views.py file, which we will discuss next.


Retrieval of Data from the Form in the views.py File

Lastly, we have our views.py, which is where we extract the data from the text input fields of the form and store them in variables, which then get processed and passed back into the template file.

So, we get the data from the form through the form.is_valid function along with the form.cleaned_data.get() function.

This is shown below.



The first thing we must do is get the data from the submit button. This is done using the request.POST.get() function, passing in the argument "submit" to the function. This gets the value of the submit button, either None initially or "Submit" after being clicked. Unlike the other data, this data does not need to be valid. It's not part of the validating data.

We then have our 3 variables, firstname, lastname, and emailvalue set equal to empty strings (''), or else we'll get an Unbound Local Variable Error.

Next, we extract data from the text input fields of the form through the form.cleaned_data.get() function, specifying the name of the form field in this function. We simply store these values in variables.

However, we must pass the form through the is_valid() function. What this is_valid() function does is it checks first (before we get the data) whether the data is valid or not.

If the data is not valid, we don't want to extract it. This is the power of using Django forms. It can check to see whether the data is valid or not without having to write external functions to validate (though we can if we want to in order to create extra validation).

The is_valid() function makes sure that the data is valid. If it is, then we can get the cleaned data.

Back to the Django forms.

So we create the variable, firstname, and set it equal to form.cleaned_data.get('first_name'). This retrieves the data that the user entered into the first_name form field.

We create the variable, lastname, and set it equal to form.cleaned_data.get('last_name'). This retrieves the data that the user entered into the last_name form field.

We create the variable, emailvalue, and set it equal to form.cleaned_data.get('email'). This retrieves the data that the user entered into the email form field.

These are all of our variables. We then pass them into the template file through the context dictionary.

This is how we are then able to render out the results in the template file.

And this is how we can retrieve data from a Django form. This applies to all types of form fields, looking radio buttons, text areas, check boxes, etc. We can extract data from these form fields using the form.cleaned_data.get() function.

And then we can process this data any way we want to.

So this is how to retrieve data from form fields of a form using Python in the Django form class.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...