crispy-bootstrap5
crispy-bootstrap5 is a Bootstrap 5 template pack for the popular `django-crispy-forms` library. It enables Django forms to be rendered with Bootstrap 5 styles and components, including features like floating labels, accordions, and switches. The library follows a CalVer versioning scheme and releases frequently to keep pace with Django and Bootstrap updates.
Warnings
- breaking Major versions frequently drop support for older Django and Python versions. Always check release notes before upgrading. For instance, version 2026.3 dropped support for Django 4.2, 5.0, 5.1 and Python 3.8, 3.9, while adding support for Django 6.0 and Python 3.14.
- breaking Support for `django-crispy-forms` 2.2 and earlier was dropped in version 2024.10 of `crispy-bootstrap5`.
- gotcha For Django 5.2 and newer, templates were updated to include `aria-describedby` for `<fieldset>` elements and a parent `<div>` for errors, improving accessibility. Older versions might not be fully accessible or compatible with Django 5.2's expectations without these changes.
- gotcha When using `FormHelper` to define complex layouts in your form, you should use the `{% crispy form %}` template tag instead of the `{{ form|crispy }}` filter. The filter provides basic rendering, similar to `as_p`, `as_ul`, or `as_table`, and does not allow for layout customization defined in `FormHelper`.
- deprecated Pre-CalVer versions (0.x) used a different versioning scheme and may not receive updates or support. The project switched to CalVer (YYYY.Minor) with version 2023.10.
Install
-
pip install crispy-bootstrap5 django-crispy-forms
Imports
- FormHelper
from crispy_forms.helper import FormHelper
- Layout
from crispy_forms.layout import Layout
- FloatingField
from crispy_bootstrap5.bootstrap5 import FloatingField
- render_crispy_form
from crispy_forms.utils import render_crispy_form
Quickstart
# settings.py
INSTALLED_APPS = [
# ...
'crispy_forms',
'crispy_bootstrap5',
# ...
]
CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
CRISPY_TEMPLATE_PACK = 'bootstrap5'
# forms.py (example form)
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit
class ContactForm(forms.Form):
name = forms.CharField(label='Your Name', max_length=100)
email = forms.EmailField(label='Your Email')
message = forms.CharField(label='Your Message', widget=forms.Textarea)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
'name',
'email',
'message',
Submit('submit', 'Submit', css_class='btn btn-primary')
)
# views.py (example view)
from django.shortcuts import render
from .forms import ContactForm
def contact_view(request):
form = ContactForm()
return render(request, 'contact.html', {'form': form})
# templates/contact.html
{% load crispy_forms_tags %}
<form method="post">
{% csrf_token %}
{% crispy form %}
</form>