This is a quick tutorial to get you start with django-crispy-forms and never look back. Crispy-forms is a great application that gives you control over how you render Django forms, without breaking the default behavior. This tutorial is going to be tailored towards Bootstrap 4, but it can also be used with older Bootstrap versions as well as with the Foundation framework.
The main reason why I like to use it on my projects is because you can simply render a Django form using `` and it will be nicely rendered with Bootstrap 4, with very minimal setup. It’s a really life saver.
Installation
Install it using pip:
Add it to your INSTALLED_APPS
and select which styles to use:
settings.py
Setup Bootstrap
You can either download the latest Bootstrap 4 version at getbootstrap.com. In that case, go to download page and get the Compiled CSS and JS version.
Or you can use the hosted Bootstrap CDN:
For simplicity, I will be using the CDN version. Here is my base.html template that will be referenced in the following examples:
I only added the CSS file because we won’t be using any JavaScript feature.
Basic Usage
Suppose we have a model named Person
as follows:
models.py
Let’s say we wanted to create a view to add new Person
objects. In that case we could use the built-in CreateView
:
views.py
Without any further change, Django will try to use a template named people/person_form.html
. In that case “people”
is the name of my Django app:
people/person_form.html
This is a very basic form rendering, and as it is, Django will render it like this, with no style, just plain form fields:
To render the same form using Bootstrap 4 CSS classes you can do the following:
people/person_form.html
Now the result, much better:
There are some cases where you may want more freedom to render your fields. You can do so by rendering the fields
manually and using the as_crispy_field
template filter:
And the result is something like the screen shot below:
Form Helpers
The django-crispy-forms app have a special class named FormHelper
to make your life easier and to give you complete
control over how you want to render your forms.
Here is an example of an update view:
forms.py
The job is done inside the __init__()
method. The rest is just a regular Django model form. Here I’m defining that
this form should handle the request using the POST method and the form should have an submit button with label
“Save person”.
Now our view, just regular Django code:
views.py
Then in our template:
people/person_update_form.html
Here we can simply call the {% crispy %}
template tag and pass our form instance as parameter.
And that’s all you need to render the form:
Conclusions
That’s pretty much it for the basics. Honestly that’s about all that I use. Usually I don’t even go for the
FormHelper
objects. But there are much more about it. If you are interested, you can check their official
documentation: django-crispy-forms.readthedocs.io.
If you are not sure about where you should create a certain file, or want to explore the sample project I created for this tutorial, you can grab the source code on GitHub at github.com/sibtc/bootstrap-forms-example.