Develop your first web application in Django 1.10 – Part 4

Learn how to use Django Forms to submit user input data into Database.

In previous post I discussed about Db interaction with a Django application and how to pass data in a html view and display it. Now we will move onto next step and will learn how to use HTML forms to store data in database.

Django Forms

The form I am going to deal is.. yeah, the project form so that I can submit project details. I will create a new folder under tracker/templates and will call it project. The reason to do is that I want to view file separate w.r.t modules. In the file I am going to add following HTML:

The first line is extended the template and enclosing the FORM html in block content. This is a typical bootstrap form. You should notice {% csrf_token %}. If you have worked on MVC frameworks before then this should not be alien for you but if you have not, CSRF stands for Cross-Site Request Forgery  is a kind of attack that let makes site to submit information on behalf of user and since the server has no idea, it believes it was the user who submitted the info. CSRF Token is a kind of ticket or pass for each request; when a form is submitted, the server issues a token for that particular request and when the user submits form, if it finds that token, it welcomes otherwise kick the user out by considering it a hijacked request.

The form is ready, next up, we need to create a new method in views.py.

render accepts the form I just created so that it can be viewed on browser. But, how? I mean which URL should I use to access it? hmm. Yeah right, we need to add another entry in tracker/urls.py. Before I do that, a little correction in main urls.py

Earlier there was a $ sign in the end for tracker urls which will not let append new subfolders since $ closes the boundary. I just removed it and then I move on to tracker/urls.py and add the following entry:

Now.. if I go to http://127.0.0.1:8000 then I will be seeing the following screen:

 

If you notice I added a new description field. We need this field in our database as well. So, let’s make changes in models.py file:

Then run makemigrations command:

Migration file is generated. Now it’s time to run python manage.py migrate. On running it gave the error:

The thing is, I did not let the field accept null. So I changed it to:

And it worked. I was setting blank but not null and there’s definitely difference between a null and blank.

Now I am going to set up Form class in forms.py. Create tracker/forms.py and in the file write:

If you notice it is similar to models.py. Here I mentioned labels, help text etc. In widget attribute we pass supporting attributes details like class etc. widget=forms.Textarea tells how to render a certain field. Now open views.py and make changes in add_new_project.

First import the from .forms import ProjectForm. We created the form instance and then checking whether the request is GET or POST. If yes, send all POSTed vars to ProjectForm and then check validity, if valid then sanitize input fields. Before I go further and save the record, let’s discuss the render() method here which is not unfamiliar by now. Pass form instance to your template and now your template should be rendered like it:

I am iterating the form fields, label is being set for labeling and the html itself is being set by mentioning {{field}}. If all goes well your form will look like:

Now we have required data, it’s time to save it. I will be using plain old objects.create here.add_new_project will now look like this:

Once I have the cleaned data, I m checking whether record inserted by getting pk field of the new record, if exist, I am returning to index. Time to test. I filled up the form:

And if it’s submitted well then I am seeing the result on main page:

Yay!!

So you saw how to add a new field in model and then rendering and submitted a form by creating a Form class. This should be enough to figure out form submission in Django.

In next post which will probably be the last post of this series, I will discuss Django’s most powerful feature, the Admin Panel

As usual code is available on Github.

 

If you like this post then you should subscribe to my blog for future updates.

* indicates required




If you like my work then you can donate me in bitcoins