DjangoCon Europe 2023 | Good form: How Django’s form rendering improved during the 4.x series

Mike Percy

Improve your form rendering in Django with the latest features and best practices, including use of field sets, templates, and third-party packages like Crispy Forms.

Key takeaways
  • Django 4.1 introduces use field set method for bound field template rendering.
  • Field sets were added in Django 5.0 and replaced the old style legend tags.
  • To ease transition, deprecated old style is still available.
  • Crispy Forms is a third-party package that adds customization options for form rendering.
  • Template rendering has been improved, and it’s recommended to use as_div instead of as_table.
  • The field order attribute can be used to reorder fields on a form.
  • Custom form renderers can be created to define a template name and render the form.
  • Bootstrap forms come with additional features, including custom layout objects and widget wrappers.
  • The legend tag has been introduced in Django 4.1, which allows for more customization options.
  • The attrs dict on a widget can be used to define CSS classes.
  • Using third-party packages like Crispy Forms can be useful for adding additional features.
  • The bound_field object knows everything needed to render a field, including its label, errors, and help text.
  • The form renderer can be set to use a specific template, and this can be overridden on a per-form basis.
  • The bootstrap_template pack comes with a template for rendering forms as HTML.
  • The bootstrap alerts component can be used to render alert messages in a form.
  • The bootstrap_float component can be used to make fields float to the right in a form.
  • The bootstrap_labels component can be used to add custom labels to fields.
  • The bootstrap_notifications component can be used to add notifications to forms.
  • The bootstrap_selects component can be used to add selects (dropdowns) to forms.