Crispy Bootstrap3
Crispy Bootstrap3 is a template pack for `django-crispy-forms` that enables rendering Django forms with Bootstrap 3 styling. This package was originally included within the core `django-crispy-forms` library but was extracted into a standalone distribution starting with `django-crispy-forms` version 2.0. It provides a set of templates to integrate Bootstrap 3's grid system and form components into Django forms with minimal effort. The current version is 2024.1, with releases generally aligning with `django-crispy-forms` updates and continued Bootstrap 3 compatibility.
Common errors
-
ModuleNotFoundError: No module named 'crispy_bootstrap3'
cause The `crispy-bootstrap3` package has not been installed or has not been added to your `INSTALLED_APPS` in `settings.py` after upgrading `django-crispy-forms` to version 2.0 or newer.fixRun `pip install crispy-bootstrap3` and ensure `'crispy_bootstrap3'` is listed in your `INSTALLED_APPS` in `settings.py`. -
django.template.exceptions.TemplateDoesNotExist: bootstrap3/uni_form.html (or similar path)
cause This typically means `crispy_bootstrap3` is either not installed, not added to `INSTALLED_APPS`, or the `CRISPY_TEMPLATE_PACK` setting is incorrect or pointing to a non-existent template pack. It can also occur if `django-crispy-forms` is too old (pre-2.0) and doesn't expect template packs to be separate.fixVerify `crispy-bootstrap3` is installed and in `INSTALLED_APPS`. Check `CRISPY_TEMPLATE_PACK = 'bootstrap3'` in `settings.py`. Ensure `django-crispy-forms` is version 2.0 or higher. -
Forms are not styled correctly (e.g., no Bootstrap classes applied, plain HTML)
cause The Bootstrap 3 CSS/JS files are missing from your HTML templates, or the `CRISPY_TEMPLATE_PACK` setting is not correctly configured to 'bootstrap3', or a frontend framework (e.g. CDN) is using a different version of Bootstrap.fixConfirm Bootstrap 3 CSS and JavaScript are loaded in your base HTML. Double-check `CRISPY_TEMPLATE_PACK = 'bootstrap3'` in `settings.py`. Ensure `{% load crispy_forms_tags %}` is present in your template.
Warnings
- breaking Template packs, including `crispy-bootstrap3`, were extracted from the core `django-crispy-forms` library starting with version 2.0. If you are upgrading `django-crispy-forms` from a version prior to 2.0 to 2.0 or newer, you must now explicitly install `crispy-bootstrap3` and add `'crispy_bootstrap3'` to your `INSTALLED_APPS` in `settings.py`.
- gotcha `crispy-bootstrap3` only provides the Django template rendering logic. It does *not* include the Bootstrap 3 CSS or JavaScript files themselves. You must manually include Bootstrap 3's static files (e.g., via CDN or local installation) in your project's HTML templates for the forms to be styled correctly.
- gotcha Ensure the `CRISPY_TEMPLATE_PACK` setting in `settings.py` correctly matches the template pack you intend to use (e.g., 'bootstrap3'). A mismatch, especially if you have multiple `crispy-bootstrapX` packages installed or a CDN for a different Bootstrap version, can lead to `TemplateDoesNotExist` errors or incorrectly styled forms.
- gotcha When using inline forms with `crispy-bootstrap3`, form errors (e.g., from validation failures) might not display by default. This is because the default `inline_field.html` template may not include the necessary logic to render error messages, unlike standard field templates.
Install
-
pip install crispy-bootstrap3
Imports
- crispy_forms_tags
{% load crispy_forms_tags %}
Quickstart
import os
# settings.py
# ...
INSTALLED_APPS = [
# ...
'crispy_forms',
'crispy_bootstrap3',
# ...
]
CRISPY_ALLOWED_TEMPLATE_PACKS = 'bootstrap3'
CRISPY_TEMPLATE_PACK = 'bootstrap3'
# forms.py (example Django 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")
email = forms.EmailField(label="Your Email")
message = forms.CharField(widget=forms.Textarea, label="Your Message")
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-primary')
)
# my_template.html (example Django template)
# Make sure Bootstrap 3 CSS and JS are linked in your base template
# {% load crispy_forms_tags %}
# <form method="post">
# {% csrf_token %}
# {% crispy form %}
# </form>