{"id":2474,"library":"django-crispy-forms","title":"Django Crispy Forms","description":"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.","status":"active","version":"2.6","language":"en","source_language":"en","source_url":"https://github.com/django-crispy-forms/django-crispy-forms","tags":["django","forms","frontend","bootstrap","tailwind","bulma","semantic-ui","dry-code"],"install":[{"cmd":"pip install django-crispy-forms","lang":"bash","label":"Install core library"},{"cmd":"pip install crispy-bootstrap5","lang":"bash","label":"Install a template pack (e.g., Bootstrap 5)"}],"dependencies":[{"reason":"Core dependency for any Django application.","package":"Django","optional":false},{"reason":"Optional, provides Bootstrap 2 template pack. Required since 2.0 if using Bootstrap 2.","package":"crispy-bootstrap2","optional":true},{"reason":"Optional, provides Bootstrap 3 template pack. Required since 2.0 if using Bootstrap 3.","package":"crispy-bootstrap3","optional":true},{"reason":"Optional, provides Bootstrap 4 template pack. Required since 2.0 if using Bootstrap 4.","package":"crispy-bootstrap4","optional":true},{"reason":"Optional, provides Bootstrap 5 template pack. Required since 2.0 if using Bootstrap 5.","package":"crispy-bootstrap5","optional":true}],"imports":[{"note":"Used to programmatically define form rendering behavior.","symbol":"FormHelper","correct":"from crispy_forms.helper import FormHelper"},{"note":"Primary class for defining custom form layouts.","symbol":"Layout","correct":"from crispy_forms.layout import Layout"},{"note":"Layout object to add a submit button to your form.","symbol":"Submit","correct":"from crispy_forms.layout import Submit"},{"note":"Layout object to group fields within a <div> tag.","symbol":"Div","correct":"from crispy_forms.layout import Div"},{"note":"Layout object to create a Bootstrap row for multi-column layouts.","symbol":"Row","correct":"from crispy_forms.layout import Row"},{"note":"Layout object to group fields within a <fieldset> tag.","symbol":"Fieldset","correct":"from crispy_forms.layout import Fieldset"}],"quickstart":{"code":"import os\nfrom django import forms\nfrom crispy_forms.helper import FormHelper\nfrom crispy_forms.layout import Layout, Submit, Row, Column\n\n# settings.py (excerpt)\n# INSTALLED_APPS = [\n#     ...,\n#     'crispy_forms',\n#     'crispy_bootstrap5', # Or your chosen template pack\n# ]\n# CRISPY_ALLOWED_TEMPLATE_PACKS = ['bootstrap5']\n# CRISPY_TEMPLATE_PACK = 'bootstrap5'\n\nclass ContactForm(forms.Form):\n    name = forms.CharField(label='Your Name', max_length=100)\n    email = forms.EmailField(label='Your Email')\n    message = forms.CharField(label='Your Message', widget=forms.Textarea)\n\n    def __init__(self, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n        self.helper = FormHelper()\n        self.helper.layout = Layout(\n            Row(\n                Column('name', css_class='form-group col-md-6 mb-0'),\n                Column('email', css_class='form-group col-md-6 mb-0'),\n                css_class='form-row'\n            ),\n            'message',\n            Submit('submit', 'Send Message', css_class='btn btn-primary')\n        )\n\n# views.py (excerpt)\n# from django.shortcuts import render\n# def contact_view(request):\n#     form = ContactForm()\n#     return render(request, 'contact.html', {'form': form})\n\n# contact.html (excerpt)\n# {% load crispy_forms_tags %}\n# <form method=\"post\">\n#     {% csrf_token %}\n#     {% crispy form %}\n# </form>\n\n# To make this runnable in a minimal context (not a full Django app)\n# This part is for demonstration only and assumes Django is configured\n# and settings above are set if running in a real project.\nif __name__ == '__main__':\n    # This part is purely illustrative as it requires a full Django setup\n    # and request cycle to actually render.\n    # In a real Django app, you would define this in forms.py\n    # and then render in a template using `{% crispy form %}`\n    form_instance = ContactForm()\n    print(\"Quickstart Form Helper and Layout defined successfully.\")\n    print(\"Remember to configure settings.py and your templates as per comments.\")\n","lang":"python","description":"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 %}`."},"warnings":[{"fix":"Install the appropriate template pack (e.g., `pip install crispy-bootstrap5`) and add it to `INSTALLED_APPS` and set `CRISPY_TEMPLATE_PACK` in your `settings.py`.","message":"Version 2.0 removed all built-in Bootstrap template packs. These are now standalone packages (e.g., `crispy-bootstrap5`).","severity":"breaking","affected_versions":">=2.0"},{"fix":"Upgrade your Django version to a supported one (e.g., Django 5.2 or later as per 2.4 release, or 6.0 as per 2.5 release) or downgrade `django-crispy-forms` to a compatible version.","message":"Version 2.6 dropped support for Django 4.2, 5.0, and 5.1.","severity":"breaking","affected_versions":">=2.6"},{"fix":"Ensure your project uses Python 3.9 or newer for `django-crispy-forms` 2.5+, and Python 3.8 or newer for `django-crispy-forms` 2.1-2.4. Python 3.10 is the minimum requirement for 2.6.","message":"Version 2.5 dropped support for Python 3.8. Version 2.1 dropped support for Python 3.7.","severity":"breaking","affected_versions":">=2.1 (Python 3.7), >=2.5 (Python 3.8)"},{"fix":"Manually link the CSS and JavaScript files for your chosen CSS framework (e.g., Bootstrap) in your base templates. Refer to your CSS framework's documentation for correct setup.","message":"Crispy Forms does not include static files (CSS/JS) for template packs. You must include them yourself.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure that `{{ form.errors }}` or `{{ form|as_crispy_errors }}` is included in your template to display non-field errors. For field-specific errors, ensure your layout objects are correctly structured to allow their display.","message":"Form validation errors may not appear if you are using complex layout objects or specific template customizations.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Add `CRISPY_TEMPLATE_PACK = 'your_template_pack'` (e.g., `'bootstrap5'`) to your `settings.py` to define the default template pack. You can also specify it per form or even per `{% crispy %}` tag.","message":"Not setting `CRISPY_TEMPLATE_PACK` globally may lead to forms not rendering 'crispy' by default, or with an unexpected template pack.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}