How to Save Data from a Form to a Database Table in Django 


               


How to Save Data from a Form to a Database Table in Django



Python


In this article, we show how to save data from a form to a database table in Django.

So let's say that we created a database table. Maybe it's a database table saving data from a blog post.

Let's say this blog post has 4 fields, the user's first name, last name, email and comment.

So the next step is to create a ModelForm based off of this database table.

What is a ModelForm?

A ModelForm is a form created in Django that is a form whose fields are taken from a database table.

Therefore, the two are directly linked and when we create the form we don't have to specify the type and all of that, because the fields are taken directly from the database table. So we don't have redundantly declare all fields of the form.

So once we have the database table and the ModelForm, we can then save the data easily in the views.py file using the form.save() function after validating the data with the form.is_valid() function.

So we'll go through all of this in a real-life example now below.

So let's say we have a form called BlogComments, shown below.



So you can see that we have a database table called BlogComments. You know that this is a database table because it is specified, models.Model

This database table has 4 fields, firstname, lastname, email and comment.

So this is the database table that we have.

So now let's create the ModelForm based off of this database table.


Creating the ModelForm

So now we go to the forms.py file and create the ModelForm.

The code to do this is shown below.



So let's now explain this code we have above.

First we import forms from django because we're going to create a form based on the database table we created.

We also must import the database table we created in the models.py file.

We then create our form, which we'll call BlogCommentsForm.

Underneath this we have class Meta. What class meta is data that allows us to explicitly tell Django which fields of the database table to include in the form. Two variables that may be included in this class Meta are fields and exclude.

Here we just have included Fields. In this fields list, we specify all the fields that we want to include in the form from the database table. In this example, I include all the fields from the Django database table. However, you can don't have to include all fields from the database if you don't want to. In the include list, you simply list those fields that you want to include.

So now that we've created our form field from the database table, we just now have to go to the views.py file and pass in the form to view, so that users can go in and enter data into the form and the data can be saved.


views.py File

So now that we've created our form from the database table in the models.py file, the last thing to do is just to render the file into view and also save the data to our database.

This is what we do in the code below.



So in our views.py file, we import render to render our HTML template file.

Then we import the form that we created in our forms.py file, based off of our database table. The form is named BlogCommentsForm.

We then create a function called showform().

We create a variable called form and set it equal to BlogCommentsForm(request.POST or None). The reason we set it equal to request.POST or None is so that it retains the data that a user enters into the form after the submit button is pressed. or None is so that it doesn't raise validation errors before a user has pressed the submit button.

We then write that if the form is valid, we save the form using the statement, form.save(). This statement saves the form data to the database.

We then pass in the form into the context dictionary and this context dictionary gets renders into the HTML template file.

Now any time you go to this form and enter in data and click 'Submit', the data gets saved right to the database.

So this shows all the simplicity of saving form data to a database in Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...