Crispy Bootstrap3

2024.1 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

To integrate `crispy-bootstrap3`, first install the package. Then, add `'crispy_forms'` and `'crispy_bootstrap3'` to your `INSTALLED_APPS` in your Django `settings.py` file. Crucially, set `CRISPY_ALLOWED_TEMPLATE_PACKS = 'bootstrap3'` and `CRISPY_TEMPLATE_PACK = 'bootstrap3'` to activate the Bootstrap 3 template pack. In your Django templates, load the `crispy_forms_tags` library and render your form using `{% crispy form %}` for a complete form, or `{{ field|as_crispy_field }}` for individual fields. Remember to include Bootstrap 3's own CSS and JavaScript files in your project, as `crispy-bootstrap3` only provides the form rendering templates.

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>

view raw JSON →