Django Crispy Forms

2.6 · active · verified Fri Apr 10

Django Crispy Forms is a powerful third-party Django application that allows you to easily control the rendering behavior of your Django forms in a DRY (Don't Repeat Yourself) and elegant way. It provides tools to add CSS classes, customize layouts, and enhance form rendering with minimal effort. The current version is 2.6, and it maintains an active release cadence with several updates per year to support new Django and Python versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a Django form with `FormHelper` and `Layout` objects to customize its appearance using `django-crispy-forms`. It includes setting up a two-column row and a submit button. Remember to add `crispy_forms` and your chosen template pack (e.g., `crispy_bootstrap5`) to `INSTALLED_APPS` and configure `CRISPY_TEMPLATE_PACK` in your `settings.py`. In your template, load `crispy_forms_tags` and render the form using `{% crispy form %}`.

import os
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit, Row, Column

# settings.py (excerpt)
# INSTALLED_APPS = [
#     ...,
#     'crispy_forms',
#     'crispy_bootstrap5', # Or your chosen template pack
# ]
# CRISPY_ALLOWED_TEMPLATE_PACKS = ['bootstrap5']
# CRISPY_TEMPLATE_PACK = 'bootstrap5'

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(
            Row(
                Column('name', css_class='form-group col-md-6 mb-0'),
                Column('email', css_class='form-group col-md-6 mb-0'),
                css_class='form-row'
            ),
            'message',
            Submit('submit', 'Send Message', css_class='btn btn-primary')
        )

# views.py (excerpt)
# from django.shortcuts import render
# def contact_view(request):
#     form = ContactForm()
#     return render(request, 'contact.html', {'form': form})

# contact.html (excerpt)
# {% load crispy_forms_tags %}
# <form method="post">
#     {% csrf_token %}
#     {% crispy form %}
# </form>

# To make this runnable in a minimal context (not a full Django app)
# This part is for demonstration only and assumes Django is configured
# and settings above are set if running in a real project.
if __name__ == '__main__':
    # This part is purely illustrative as it requires a full Django setup
    # and request cycle to actually render.
    # In a real Django app, you would define this in forms.py
    # and then render in a template using `{% crispy form %}`
    form_instance = ContactForm()
    print("Quickstart Form Helper and Layout defined successfully.")
    print("Remember to configure settings.py and your templates as per comments.")

view raw JSON →