Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to customize Django forms using Django widget tweaks

Njoku Ifeanyi Gerald

What are Django forms?

Django forms are like an advanced HTMLHyperText Markup Language form. The main differences are that these forms are from Django and are pythonic in nature.

Rendering Django forms in the template might be messy at times. However, with a good knowledge of Django forms and attributes of fields, you can easily create an excellent form with powerful features.

What is a Django widget tweak?

The Django widget tweaks is used to render form fields in templates. This allows you to adjust the properties of the form (such as method and CSS class) on the back-end without rewriting the template.

Let’s start with the installation.

Step 1

pip install pipenv 

pipenv shell
pipenv install django

Step 2

Use ./ at the end to create the project in the same directory. Then, use startapp codebase to create an app for the project.

django-admin startproject DjangoWidgetTweak ./

python startapp codebase

Step 3

python migrate
python runserver

Go to and paste the following:


    # adding the codebase app
    # adding the widget tweak in the installed app

Step 4

In the codebase app folder, go to to create a model for the database.

from django.db import models

class WidgetModel(models.Model):
    name = models.CharField(max_length=400)
    age = models.IntegerField()

    def __str__(self):

Step 5

Go to the file to register the model in the admin view.

from django.contrib import admin
from .models import WidgetModel

Step 6

Create a file and name it in order to make use of Django forms.

from django import forms
from django.db.models import fields
from .models import WidgetModel

class WidgetForm(forms.ModelForm):
    class Meta:
        model = WidgetModel
        fields = '__all__'

Step 7

The file:

from django.shortcuts import redirect, render
from .forms import WidgetForm
from django.http import HttpResponse

def home(request):
    form = WidgetForm(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            return redirect('home')
            return HttpResponse('form not valid')
    context = {
        'form': form
    return render(request, 'app/home.html', context)

Step 8

In the codebase app, create a folder and name it templates.

Inside the templates folder, create another folder and name it app.

Inside the app folder, create the home.html file.

Step 9

In the DjangoWidgetTweak folder, in the file, do the following:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    # including the codebase folder '' file
    path('', include('codebase.urls'))

Step 10

Then in the codebase folder, create a file and name it</a>:

from django.urls import path
#importing home function from
from .views import home

urlpatterns = [
    path('', home, name='home'),

Step 11

Run the following commands:

  • makemigrations to prepare the model for migrations.

  • migrate to move the model and any other related packages that need to be moved to the database.

python makemigrations

python migrate

Step 12

In case you want to create a super-user, run the following command.

python createsuperuser

Step 13

After it, to start the server, run this:

python runserver

Step 14

Then, go to and login in order to access the admin panel.




Njoku Ifeanyi Gerald

View all Courses

Keep Exploring