How to customize Django forms using Django Widget Tweaks ?
Last Updated :
16 Jan, 2023
Django forms are a great feature to create usable forms with just few lines of code. But Django doesn’t easily let us edit the form for good designs. Here, we will see one of the ways to customize Django forms, So they will look according to our wish on our HTML page. Basically we will check the methods to include our own custom css, classes, or id to individual fields in forms.
Let’s say, we are having a simple Django form that will have four fields:
- First Name
- Last Name
- Username (available default)
- Password (available default)
- Confirm Password (available default)
We are not going to discuss how we create this form but rather than we are going to see how to customize the frontend of Django forms. To create a form in Django you can check out – How to create a form using Django Forms ?
The initial Django forms (available default looks like)

The default Django form
which will have a simple code like
HTML
{{form}}
< input type = "submit" value = "submit" >
|
Of course, there are many default methods that Django provide us by default like using form.as_p which looks like

Django form using as_p
that will have a simple code like
HTML
{{form.as_p}}
< input type = "submit" value = "submit" >
|
But we need to add custom classes and CSS to forms.
How to customize Django forms using Django Widget Tweaks ?
So, Let’s see how to make it look good like this

Django form input form customized
So we are going to implement it using Django Widget Tweaks. Let’s install django-widget-tweaks using pip
pip install django-widget-tweaks
Now go to the INSTALLED_APPS in settings.py and add widget_tweaks in INSTALLED_APPS
INSTALLED_APPS = [
'django.contrib.auth',
#...........(some more apps already here maybe)......
'widget_tweaks',
#...........(some more apps already here maybe)......
]
So Now we have Django widget tweaks installed let’s import it in the HTML file in which we are working write this {% load widget_tweaks %} in the top of your HTML page and we have to simply change Every field:
HTML
{% load widget_tweaks %}
< div class = "form-group" >
// first_name is the name by which first name is created in django forms
{% render_field form.first_name class="form-control" placeholder="First Name" type="text" %}
</ div >
< div class = "form-group" >
// last_name is the name by which last name is created in django forms
{% render_field form.last_name type="text" class="form-control" placeholder="Last Name" %}
</ div >
< div class = "form-group" >
// username is the default name of username in django forms
{% render_field form.username type="text" class="form-control" id="exampleInputUsername" placeholder="Username" %}
</ div >
< div class = "form-group" >
// password1 is the default name of password in django forms
{% render_field form.password1 type="password" class="form-control" id="exampleInputPassword" placeholder="Password" %}
</ div >
< div class = "form-group" >
// password2 is the default name of confirm password in django forms
{% render_field form.password2 type="password" class="form-control" id="exampleInputPassword1" placeholder="Confirm Password" %}
</ div >
< div >
< button type = "submit" value = "Submit" > Register </ button >
</ div >
|
Each field’s name has been explained in “// ” in code. In the CSS file, we will have all our code for customizing each field by class or id.
So let’s see how Django tweaks help us See in each of the fields, we have our class because of which we can add any CSS by our choice. So, we have to just design the HTML with css, and use it in Django by just replacing the input tag with render_field and form.field_name with {% %} at the end and design it with our choice.
Similar Reads
Styling Django Forms with django-crispy-forms
Django by default doesn't provide any Django form styling method due to which it takes a lot of effort and precious time to beautifully style a form. django-crispy-forms solves this problem for us. It will let you control the rendering behavior of your Django forms in a very elegant and DRY way. Mod
1 min read
How to create a form using Django Forms ?
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. This post revolves around how to create a basic form using various Form Fields and attributes. Creating a form in Django is completely similar to creating a model
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
Custom Field Validations in Django Forms
This article revolves around how to add custom validation to a particular field. For example to add validation of an email to a CharField by specifying a particular format. There can be multiple ways on how to achieve custom validation. In this article, we are going to show it from the form itself s
2 min read
How to use Django Field Choices ?
Django Field Choices. According to documentation Field Choices are a sequence consisting itself of iterables of exactly two items (e.g. [(A, B), (A, B) ...]) to use as choices for some field. For example, consider a field semester which can have options as { 1, 2, 3, 4, 5, 6 } only. Choices limits t
2 min read
How to Integrate Custom Rich Text-Editor in Your Django Website?
In this article, we will implement tinymce text editor in our Django application. What is django-tinymce? django-tinymce is a Django application that contains a widget to render a form field as a TinyMCE editor. Features :- Use as a form widget or with a view.Enhanced support for content languages.I
3 min read
How to change border color in Tkinter widget?
Prerequisites: Tkinter GUI, Tkinter Widgets Tkinter is Pythonâs standard GUI package which provides us with a variety of common GUI elements such as buttons, menus, and various kinds of entry fields and display areas which we can use to build out an interface. These elements are called Tkinter Widge
3 min read
How to Add HTML Attributes to Input Fields in Django Forms?
Django provides an easy-to-use system for creating and managing forms through its forms.Form and forms.ModelForm classes. These classes allow us to create form fields that map directly to HTML input fields, such as <input>, <textarea>, <select>, etc. By default, these fields come w
4 min read
Django Tutorial | Learn Django Framework
Django, built with Python, is designed to help developers build secure, scalable, and feature-rich web applications quickly and efficiently. Whether you're a beginner looking to create your first dynamic website or an experienced developer aiming to enhance your skills, this tutorial will guide you
11 min read
Handle Multiple Forms on a Single Page in Django
One common scenario in web development is handling multiple forms on a single page. This might be required when a user needs to fill out various types of information without navigating away from the page. In this article, we will explore the best practices for managing multiple forms on a single pag
6 min read