I have been struggling with this issue of yours and the given answer, although they might be relevant a few years ago, did not satisfy me.
So I went myself building a hack to make crispy forms work with Django 3.0.2 (I did not test the others versions, please let me know guys if there is any issue on any other version).
Install the relevant packages to make crispy forms work with Jinja2 :
pip install django-crispy-forms django-jinja
In your project directory (and NOT your application directory), make sure you have a jinja2.py
file with:
from crispy_forms.utils import render_crispy_form
from django.contrib import messages
from django.contrib.staticfiles.storage import staticfiles_storage
from django.urls import reverse
from django.utils import translation
from jinja2 import Environment
def environment(**options):
env = Environment(
extensions=["jinja2.ext.i18n", "jinja2.ext.with_"], **options
)
env.globals.update(
{
"get_messages": messages.get_messages,
"static": staticfiles_storage.url,
"crispy": render_crispy_form, # THIS LINE IS RELEVANT
"url": reverse,
}
)
env.install_gettext_translations(translation)
return env
Make sure that your jinja2 template engine loads this option in your django's settings
:
TEMPLATES = [
{
"BACKEND": "django.template.backends.jinja2.Jinja2",
"DIRS": [os.path.join(BASE_DIR, "jinja2")],
"APP_DIRS": True,
"OPTIONS": {"environment": "your_django_project.jinja2.environment"},
},
# ...
Note the "crispy": render_crispy_form,
which will help you use crispy forms in your jinja2 templates.
Then, in your template, you can use:
{{ crispy(form) }}