liveBook · Manning

Django-crispy-forms

As explained in Django in Action

django-crispy-forms is a Django library for rendering and customizing styled HTML forms through reusable helpers, layouts, and template packs.

django-crispy-forms helps Django applications replace plain, vanilla form output with reusable, styled HTML components. It separates the core form helper from template packs for interface toolkits such as Bootstrap and Tailwind. section 13.3 section 13.3.2

A Bootstrap 5 setup requires the core package, a template pack, and Django configuration:

  • Install the main package with `python -m pip install django-crispy-forms` and register `crispy_forms` in `INSTALLED_APPS`. section 13.3.2
  • Install the Bootstrap 5 pack with `python -m pip install crispy-bootstrap5` and register `crispy_bootstrap5`. section 13.3.2
  • Set `CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"` and `CRISPY_TEMPLATE_PACK = "bootstrap5"`. section 13.3.2

Choose between quick rendering and fine-grained control in a Django template:

  • Apply the `|crispy` filter to a form object instead of `form.as_p` for an opinionated layout and template-pack styling. section 13.3.2
  • Use the `{% crispy %}` tag with a form object for greater control over HTML, including IDs, CSS classes, form action, and HTTP method. section 13.3.2
  • Add `FormHelper` to a `ModelForm` and pair it with a `Layout` object to control the rendered form. section 13.3.2

Two levels of form styling

A simple template can render a configured form with `{{ form|crispy }}`. When the page needs a custom action, method, IDs, or CSS classes, move to the `{% crispy %}` tag and configure the form through `FormHelper` and `Layout`. section 13.3.2

How Django in Action covers it

How deep it goes
Explains installation, template usage, and helper-based customization, but stops short of detailing complete Layout syntax.
Examples
Uses `form.as_p`, the `|crispy` filter, the `{% crispy %}` tag, and Bootstrap 5 configuration as concrete examples.
What it assumes
Assumes Django forms and templates, while distinguishing vanilla form rendering from reusable styled rendering.
What you can do after
Apply it via Django templates and template packs to render styled forms or customize their HTML with helpers and layouts.
How it connects
Connects Django forms and ModelForm to templates, FormHelper, Layout objects, and Bootstrap or Tailwind template packs.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.