Python Django : Handling Html Forms
forms are an important component in web programming. They are used to create a resource in the server when the form is submitted. A form can request differents type of data from the user that can be passed to the server and use later.
In this article, we create an HTML form which requests some data from user and after submitting this form, data are save in database which we can use later.
First, create a basic form template which asks for user some information, In this example we just stick with basic details like name and age but you can write more ( you should know the basic HTML).
Now look at the below code how to handle this form.
There are lots of details in the above image. Let cover one by one
In the index.html, we create a form which has labels, the input field, and a submit button. two things new in this form, action attribute, and {% csrf_token %}.
action attribute in form is just like href in an anchor tag, which contains the path/url that is followed when form is submitted.
Next is {% csrf_token %}, this one is essential for Cross Site Request Forgery Protection. Django provides many security features out of the box, csrf_token is one of these.
{% csrf_token %} is compulsory whenever you try to submit the form via post method. If you want to exercise just change the method to GET and omit the {% csrf_token %} and try to submit, yes it works, but look at the urlbar it looks like this.
It contains the data which user submitted via the form If details are private like password and userID you should always use POST method instead of GET. To use POST method you need to include {% csrf_token %} in your form.
In the views we are returning just the HttpResponse instead of html page.
Now save the data to the database which user send via form submission and render a page with all user data.
In the above image, we create a model, Myuser with name and age field, In temp function of views.py we make some edit, first check for
request method if not POST then return an error message. ( if someone tries to goes /formSubmit path by typing in browser urlbar then it is a GET request and form won%u2019t be submitted).
request method if not POST then return an error message. ( if someone tries to goes /formSubmit path by typing in browser urlbar then it is a GET request and form won%u2019t be submitted).
All the data which user submit is store in a request.POST dictionary with the key is name attribute of the input field of HTML form
After extracting name and age from form, we can print and store data in database using Myuser model we have created and return a success HttpResponse.
Now there is one more view userlist which extract all user from the database and pass in list.html via context dictionary. In
urlpatterns we add path for this view which name is userlist.
urlpatterns we add path for this view which name is userlist.
So here is our list.html, with the use of template language we can iterate and print every element in userlist.
Now if you want you can add link in this page to go to form page and add new user, you should be now able to do this.