top of page

Django Form

In the previous blog we have seen about Django templates. Now in this blog we will discuss django form. What is it and their use? Let's see.

Django form

Form is a Django class that is used to construct HTML forms. It describes a form's functionality and appearance. When we create the form in a django project the most important part is defining the fields of the form. Each field has its own set of validation rules, as well as a few other hooks. This article discusses the many fields that can be used in a form, as well as Django Forms' capabilities and approaches. Forms are mostly used to collect data from users in some form and then utilise that data to perform logical operations on databases. For instance, registering a user by collecting information such as his name, email address, and password.

Lets see an example, in which we are creating some fields too.

Lets see an example,

from django import forms

# creating a form
class Sample_form(forms.Form):
	title = forms.CharField()
	description = forms.CharField()

Django form

Creating Django form

Creating a django form is just like creating the django model. It requires specifying what field would exist in the form and of what type. For example first name last name etc.

from django import forms

# creating a form
class Create_New_form(forms.Form):

	first_name = forms.CharField(max_length = 200)
	last_name = forms.CharField(max_length = 200)
	roll_number = forms.IntegerField(
					help_text = "Enter 6 digit roll number"
	password = forms.CharField(widget = forms.PasswordInput())

Djnago rendering

Django form fields include various built-in methods to make the developer's job easier, however customising the User Interface occasionally necessitates manual implementation (UI). Django form fields can be rendered using one of three built-in techniques in a form.

from django.shortcuts import render
from .forms import Create_New_form
# Create your views here.
def sample_view(request):
    context ={}
    context['form']= InputForm()
    return render(request, "home.html", context)

It required to create instance of the form class created above in form

Code :

<form action = "" method = "post">
    {% csrf_token %}
    {{form }}
    <input type="submit" value=Submit">

How Codersarts can Help you in Django ?

Codersarts provide:

  • Django Assignment help

  • Django Error Resolving Help

  • Mentorship in Django from Experts

  • Dajngo Development Project

If you are looking for any kind of Help in Django Contact us


bottom of page