Last Updated : 12 Jul, 2025
Django Forms are used to gather input from users, validate that input, and process it, often saving the data to the database. For example, when registering a user, a form collects information like name, email, and password.
Django automatically maps form fields to corresponding HTML input elements. It also handles:
While you can build forms using raw HTML, Django Forms simplify form creation and validation, making your code cleaner and more secure.
Syntax:Note that all types of work done by forms in Django can be done with advanced HTML stuff, but Django makes it easier and efficient especially the validation part. Once you get hold of forms in Django you will just forget about HTML forms.
Django form fields are defined much like Django model fields:
field_name = forms.FieldType(**options)
Befor creating forms, ensure that a django project and app is already set up, refer to the following article to learn how to do it:
Creating a Simple Django Form
Creating a form in Django is completely similar to creating a model, one needs to specify what fields would exist in the form and of what type. For example, to input, a registration form one might need First Name (CharField), Roll Number (IntegerField), and so on.
Syntax:from django import forms
class FormName(forms.Form):
# each field would be mapped as an input field in HTML
field_name = forms.Field(**options)
To create a form, create a forms.py file in you app folder:
Python
from django import forms
class InputForm(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())
Render Django Forms
Django form fields have several built-in methods to ease the work of the developer but sometimes one needs to implement things manually for customizing User Interface(UI). A form comes with 3 in-built methods that can be used to render Django form fields.
To render this form into a view, move to views.py and create a home_view as below.
Python
from django.shortcuts import render
from .forms import InputForm
# Create your views here.
def home_view(request):
context ={}
context['form']= InputForm()
return render(request, "home.html", context)
In view, one needs to just create an instance of the form class created above in forms.py. Now let's edit templates > home.html
html
<form action = "" method = "post">
{% csrf_token %}
{{form }}
<input type="submit" value=Submit">
</form>
Now, visit http://localhost:8000/
Create Django Form from ModelsTo check how to use the data rendered by Django Forms visit Render Django Form Fields
Django ModelForm is a class that is used to directly convert a model into a Django form. If you’re building a database-driven app, chances are you’ll have forms that map closely to Django models. Now when we have our project ready, create a model in geeks/models.py,
Python
# import the standard Django Model
# from built-in library
from django.db import models
# declare a new model with a name "GeeksModel"
class GeeksModel(models.Model):
# fields of the model
title = models.CharField(max_length = 200)
description = models.TextField()
last_modified = models.DateTimeField(auto_now_add = True)
img = models.ImageField(upload_to = "images/")
# renames the instances of the model
# with their title name
def __str__(self):
return self.title
To create a form directly for this model, dive into geeks/forms.py and Enter the following code:
Python
# import form class from django
from django import forms
# import GeeksModel from models.py
from .models import GeeksModel
# create a ModelForm
class GeeksForm(forms.ModelForm):
# specify the name of model to use
class Meta:
model = GeeksModel
fields = "__all__"
Now visit http://127.0.0.1:8000/
Django Forms Data Types and Fields ListRead Next: Python | Form validation using django
The most important part of a form and the only required part is the list of fields it defines. Fields are specified by class attributes. Here is a list of all Form Field types used in Django
Core Field ArgumentsCore Field arguments are the arguments given to each field for applying some constraint or imparting a particular characteristic to a particular Field. For example, adding an argument required = False to CharField will enable it to be left blank by the user. Each Field class constructor takes at least these arguments. Some Field classes take additional, field-specific arguments, but the following should always be accepted:
Field Options Description required By default, each Field class assumes the value is required, so to make it not required you need to set required=False label The label argument lets you specify the “human-friendly” label for this field. This is used when the Field is displayed in a Form. label_suffix The label_suffix argument lets you override the form’s label_suffix on a per-field basis. widget The widget argument lets you specify a Widget class to use when rendering this Field. See Widgets for more information. help_text The help_text argument lets you specify descriptive text for this Field. If you provide help_text, it will be displayed next to the Field when the Field is rendered by one of the convenience Form methods.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4