Django form field custom widgets
Last Updated :
29 Dec, 2019
A widget is Django’s representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. Whenever you specify a field on a form, Django will use a default widget that is appropriate to the type of data that is to be displayed. To find which widget is used on which field, see the documentation about
Built-in Field classes. This post revolves about the advanced use of widgets to modify the form structure and input type.
Default Widget in Form Fields
Every field has a predefined widget, for example
IntegerField has a default widget of
NumberInput. Let's demonstrate this with help of our base project geeksforgeeks.
Refer to the following articles to check how to create a project and an app in Django.
Now let's create a demo form in "geeks/forms.py",
Python3 1==
from django import forms
// creating a django form
class GeeksForm(forms.Form):
title = forms.CharField()
description = forms.CharField()
views = forms.IntegerField()
available = forms.BooleanField()
Now to render this form we need to create the view and template which will be used to display the form to user. In geeks/views.py, create a view
Python3 1==
from django.shortcuts import render
from .forms import GeeksForm
# creating a home view
def home_view(request):
context = {}
form = GeeksForm(request.POST or None)
context['form'] = form
return render(request, "home.html", context)
and in templates/home.html,
html
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
Now let's display the form by running
Python manage.py runserver
visit
http://127.0.0.1:8000/

As seen in above screenshot, there is a different type of input field for IntegerField, BooleanField, etc. One can modify this using the following ways.
Custom Django form field widgets
One can override the default widget of each field for various purposes. The list of widgets can be seen here -
Widgets | Django Documentation. To override the default widget we need to explicitly define the widget we want to assign to a field.
Make following changes to
geeks/forms.py
,
Python3 1==
from django import forms
class GeeksForm(forms.Form):
title = forms.CharField(widget = forms.Textarea)
description = forms.CharField(widget = forms.CheckboxInput)
views = forms.IntegerField(widget = forms.TextInput)
available = forms.BooleanField(widget = forms.Textarea)
Now visit
http://127.0.0.1:8000/,

Thus we can assign, any widget to any field using
widget
attribute.
Note - The validations imposed on fields would still remain same, for example even if an IntegerField is made the same as CharField, it will only accept Integer inputs.
Using Widgets to Customize DateField
widgets have a great use in Form Fields especially using Select type of widgets where one wants to limit the type and number of inputs form a user. Let's demonstrate this with help of modifying DateField. Consider forms.py as,
Python3 1==
from django import forms
class GeeksForm(forms.Form):
title = forms.CharField()
description = forms.CharField()
views = forms.IntegerField()
date = forms.DateField()
By default, DateField as widget TextInput. It can be seen as

Now let's change the widget for better and convenient input from user of a date. Add
SelectDateWidget to DateField in
forms.py
,
Python3 1==
from django import forms
class GeeksForm(forms.Form):
title = forms.CharField()
description = forms.CharField()
views = forms.IntegerField()
date = forms.DateField(widget = forms.SelectDateWidget)
Now input of date can be seen as very easy and helpful in the front end of the application. This way we can use multiple widgets for modifying the input fields.
Similar Reads
Django Form
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. I
5 min read
How to create a form using Django Forms ?
This article explains how to create a basic form using various form fields and attributes. Creating a form in Django is very similar to creating a model, you define the fields you want and specify their types. For example, a registration form might need fields like First Name (CharField), Roll Numbe
2 min read
Django ModelFormSets
Django ModelFormsets provide a powerful way to manage multiple model-based forms on a single page. They allow you to create, update, or delete multiple instances of a model at once, using a group of forms that correspond to the model's fields.Think of ModelFormsets as a collection of forms linked to
2 min read
Django ModelForm
ModelForm is a class that automatically generates a "form" from a Django model. It reduces boilerplate code by linking your form fields directly to your model fields, making form creation faster, cleaner, and less error-prone. It also provides built-in methods and validation to streamline form proce
3 min read
Render Django Forms as table
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
2 min read
Render Django Forms as paragraph
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
2 min read
Render Django Forms as list
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
2 min read
Render HTML Forms (GET & POST)
Django is often called a "Batteries Included Framework" because it provides built-in settings and features that help developers build websites rapidly and efficiently. One of the essential components in web development is handling HTML forms, a way for users to send data to the server for processing
3 min read
Django form field custom widgets
A widget is Djangoâs representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. Whenever you specify a field on a form, Django will use a default widget that is appropriate to the type o
3 min read
Initial form data _ Django Forms
When using Django forms, we may want to automatically fill in some fields with default values. This is called initial data and it's different from placeholders because it actually fills the fields. When the form is submitted, this data is treated just like anything the user types in.Django offers mu
3 min read