1 min read

Django shorts: #2 Crispy forms

Django shorts: #2 Crispy forms
Photo by Mohammad Rahmani / Unsplash

What are crispy forms?

Crispy forms is a python package which we can implement within our Django web application. It is used to improve the quality and design of our user forms.

Step 1:

First of you head over to your terminal and install django-crispy-forms via the following command:

pip install django-crispy-forms

Step 2:

Next you will need to add the django-crispy-forms to our installed apps in settings.py as follows:

INSTALLED_APPS = [

...

'crispy_forms',


]


Step 3:

You also need to be sure that you add the bootstrap crispy template pack under your installed apps:

INSTALLED_APPS = [

...

'crispy_forms',


]

CRISPY_TEMPLATE_PACK='bootstrap4'

Step 4:

Create a basic html page, as follows:

<!--index.html-->

<form method="post">

    {% csrf_token %}

    {{form}}

    <button type="submit" class="btn btn-primary"> Log in </button>

</form>

Step 5:

Add the following tag of crispy forms at the top of your html page:

<!--index.html-->

{% load crispy_forms_tags %}

Step 6:

Add the crispy forms filter to your form:

|crispy

<!--index.html-->

<form method="post">

    {% csrf_token %}

    {{form|crispy}}

    <button type="submit" class="btn btn-primary"> Log in </button>

</form>