DEV Community

Cover image for Django shorts: #3 Crispy forms
Arno Pretorius
Arno Pretorius

Posted on

Django shorts: #3 Crispy forms

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
Enter fullscreen mode Exit fullscreen mode

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',

]
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

Step 4:

Create a basic html page, as follows:

<form method="post">

    {% csrf_token %}

    {{form}}

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

</form>
Enter fullscreen mode Exit fullscreen mode

Step 5:

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

<!--index.html-->

{% load crispy_forms_tags %}
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

A final note…
For those that are interested in learning how to secure their Django web application as well as how to add 2FA, feel free to check out my latest course:

Python Django: Ultimate Web Security Checklist- 2022

Top comments (0)