We completed with MTV in our last blog where we connected our model, template and view but till now we have not worked by taking inputs from our user. Today we will be creating a basic form in Django (form has a syntax similar to our models). For now, we will be just displaying the details in our console window but later we will learn to connect this to our database. Since we already connected everything in MTV, as soon as we connect our database to our forms, we will be able to get a fully functional basic form. Let’s begin, shall we?

Similar to models, we have to create a new file named forms.py and then we have to create a class inside it. The syntax to store values is also similar to models as we have a character field, email field etc. But we have an additional attribute available in forms called widgets. They can be used to customize the appearance of our fields available. We can a create a basic form as shown below:

“text” is provided with a widget which enables it’s windows to expand as required.

Now we have to inject this form into our html with the help of template tagging. For this we have to create a view and return an instance of our form. We can create an instance of our form as, form=Formname(). Then we are adding this instance as a key in our dictionary so that we can return the rendered html later. This is done as shown:

We can just call the key specified here in our firstform.html and we will be getting a form as shown below:

But we can see that our form looks very ugly as all the fields are lying side by side, we can change it by using different specifiers with our form. For example:

form.as_ul: Display fields as unordered list

form.as_p: Display fields as paragraph in separate line

form.as_table: Display fields as table elements

I am using form.as_p, which gives my form the following look:

Let us make it better by bootstrapping. I don’t have much knowledge in this area, so let me just use the “jumbotron” class (as everyone would have heard about it) for our display. Since we have to submit our details, let us create a submit button by:

<input type=”submit” class=”btn btn-primary” value=”Submit”>

Now if we check our page, we can find a page which is pleasant to view:

As we need to submit the details, we have to put our form inside a form tag with method as post, which can be done as: <form method=”post”>. But still, we won’t be able to submit our details as there is a need of CSRF token for security purpose in Django. It stands for Cross Site Request Forgery, which makes sure that user is not transferred to some other link and the user’s data is safe. It should be declared in our form as {% csrf_token %}. When we click the submit button, all our data is stored in cleaned_data attribute of our forms. It is similar to a dictionary with key values as a field names. Also, we need to check the method while posting so as to create a different instance of our form as:

if request.method == ‘POST’:
form = FormName(request.POST)

Then we need to check the validity of our post and if it is a valid post, we can print the details to our console. As soon as we click the submit button, our form would still contain the data which we have entered. In order to remove all the data entered, we can redirect us to the same form page again as shown:

return HttpResponseRedirect(‘http://127.0.0.1:8000/fillform/')

All the above mentioned process is shown below:

We are now ready with our form, let us enter some data and check whether the same is printed in our console or not. I am entering the following data:

As soon as we click the submit button, all our details are inserted to our dictionary and the details are cleared from our page. We can find the same details printed in our console window:

So, that’s all for today. There is much more to form validation which we will be discussing later, for now you can refresh previous topics like MTV and populate your database. You can find the link for the files related to project from the following link, stay happy and keep coding…

https://github.com/ashish-sharma-260696/DjangoForm

Thank you for reading, if you liked the article please click on the clap button and show your support. You can clap more than once, just keep pressing the icon!!!

VLSI Engineer with the urge for sharing knowledge.