Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to style Django Forms with django-crispy-forms

Njoku Ifeanyi Gerald


Django doesn’t come with a form styling method, so it takes a lot of effort to style a Django form.

Django-crispy-forms solves this problem for us.

What is Django-crispy-forms?

Django-crispy-forms provides you with a crispy filter and {% crispy %} tag that lets you control the rendering behavior of your Django forms in an elegant and dry way.

You have full control without writing custom form templates. Django-crispy-forms avoids breaking the standard way of doing things in Django, as it operates well with any other form application.

How to install DjangoCrispForm

Let’s start with installation.

pip is a package manager for Python packages.

Step 1

pip install pipenv
pipenv shell
pipenv install django

Step 2

django-admin startproject DjangoCrispyForm ./
python startapp codebase

Step 3

python migrate
python runserver

Step 4

Go to and enter the following. helps register your app and packages that are being installed write rules.

# Application definition




Step 5

In the codebase app folder, go to This file is responsible for creating the model for the database.

from django.db import models

class CrispyModel(models.Model):
  songs = models.CharField(max_length=500)
  name_of_artist = models.CharField(max_length=500)
  number_of_awards = models.IntegerField()
  def __str__(self):

Step 6

Go to In this file, we register our previously created model by importing it. By doing so, we can see the model when logged in as admin.

from django.contrib import admin
from .models import CrispyModel

Step 7

Create a file and name it The forms file is responsible for creating Django forms, which is like an HTML form, but dynamic.

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

class CrispyForm(forms.ModelForm):
  class Meta:
    model = CrispyModel
    fields = '__all__'

Step 8

The file is used to create functions or classes that visualize how a route will operate.

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

def home(request):
    #referencing the Form
    #if it comes with a post request or a get request
    form = CrispyForm(request.POST or None)
    #if it is a post request
    if request.method == 'POST':
        #check if the form is valid enough to be saved
        if form.is_valid():
            #to save the form
            return redirect ('home')
            return HttpResponse('invalid form')
    context = {
        'form': form
    return render(request, 'app/home.html', context)

Step 9

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

Inside the “templates” folder, create another folder and name it “app.” Then, inside the “app” folder, create the “home.html” file.

{% load crispy_forms_tags %}
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <form method="post" action=".">
        {% csrf_token %}

Step 10

In the DjangoCrispyForm folder, enter the following in the "" file.

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

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

Step 11

Then, in the “codebase” folder, create a file and name it “”. This file is responsible for creating different routes.

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

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

Step 12

Run the following commands:

python makemigrations

python migrate

The makemigration command prepares the model to move into the database.

migrate moves the files or model to the database.

Step 13

If you want to create a super-user, run the following command:

python createsuperuser

Step 14

Run the following:

python runserver

Step 15

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




Njoku Ifeanyi Gerald

View all Courses

Keep Exploring