Using Django Forms in Charcha - HashedIn Technologies

Using Django Forms in Charcha

Technology - 03 Jun 2017
Urvi Sharma

This is the third blog in Python Django Forum Blog Series. In this blog, we will be going through the implementation of Django Forms. This post is inspired from the Django form usage in Charcha Discussion Forum. You can find the full code for the charcha forum here.

While creating interactive websites, one of the widely used way to submit data to the server will be forms. For such a use case, Django provides us the form library. It helps us to design forms and also lets us decide how they look in our template. After submission, it also helps us to process it like storing data in the database. A Django Form can be defined either from scratch or you can simply create a ModelForm, which can be returned from HTTP methods, which will therein save the data in the model’s fields. Based on our requirements we can choose from either of the aforementioned ones.

Django Forms provides us various features such as facilitating validation on the browser side and also after submitting the data, generates proper error messages and binds template objects and data models. And after submitting the form, we can directly save it to the database in the required format.

Let us assume we have a requirement to create the django models of a discussion platform like Charcha. In this blog post, we will go through the process of designing such a model which is both conforming to our requirements as well as efficient. In this blog we will see the process to define and use the Django forms. Lets discuss about charcha, an interactive website, in which we are using Django forms:

  • To create the user profile, to add user
  • In discussion pages, to add and edit the questions or comments

we are also using the Django auth user forms.

Defining forms in Django View

We first need to define the Django forms in our application. For this we need to import the Django forms. Here, in charcha we are using the Django auth forms and Django forms.


In the above-given example, we are using the forms.ModelForm coupled with Comment model along with label as Comment and we have also provided the help text. We have added a user-friendly label to the field which will be shown in the '<label>' tag when rendered in the template. We can also define some additional things like validations. We can add validations like max length and required etc. These validations puts a validation in HTML '<input>' tag which will enable client side validation in the browser. They also validate the data on the server after it’s received from the form. Form has an 'is_valid()' method which checks all the validations throughout all the fields in the form. On this method call, if all fields are verified it will return true and provide all the form’s data in the 'cleaned_data' attribute.


Linking Views and Template

Now to link the form with the view, we will use a link sample-url in our case. We import url from Django urls and function from the view which we want to use: The urls will be in the format: url(regex, view, kwargs=None, name=None) Here regex is the url which will link the template and view. view is the Django function which is the result of as_view() for the class-based views. Using kwargs we can send some additional arguments to the view.



Defining HTML Template

For a template, depending on the user who is going to view, we will use the url to define which url and view we are going to use. We will be able to do this by using <form></form> tag with attributes as some-url and methods as GET or POST depending on user requirements. Here GET or POST are the HTTP verbs. We will use GET for not so safe search, like when we want to implement the web search form or the data which is not so important even when a user views it on url. As urls can easily be edited, shared or can be opened directly. On the other hand we use POST, which does not allow user to see the data in url. It can also support larger payload as compared to the GET. It also provides us CSRF (Cross Site Request Forgery) protection, provides us more control over URL and access. That makes POST method far safer than GET.

We also need to define the submit input type field for the submission of the form. Between these, we have our form fields which user needs to fill in the given form. All the form’s fields and their attributes like validations and type of field will be unbundled into HTML from {{form}} , that we are using in the template, by Django’s template.


Summary

In above post, we get to know about usage and process of using Django forms. Django provides us a great number of prebuild tools and libraries to build a form to get input from the user and then process and respond to the input, which makes the handleing of user input very easy.

Share
Tweet
+1
Share

E-book on Digital Business Transformation