Django Widget Tweaks is a Python library that allows developers to customize the rendering of Django form fields directly within templates using template tags and filters, rather than modifying Python-level form definitions. This enables front-end developers to easily add CSS classes and HTML attributes. It is actively maintained by Jazzband, with version 1.5.1 supporting Django 5.0 and released annually or more frequently.
pip install django-widget-tweaksVerified import paths — ran on the pinned version, not inferred.
After installing, add 'widget_tweaks' to your Django project's `INSTALLED_APPS` in `settings.py`. Then, in your Django templates, load the `widget_tweaks` library using `{% load widget_tweaks %}`. You can then use the `render_field` tag or various filters (like `add_class`) to customize form fields, adding HTML attributes and CSS classes directly in the template. This example demonstrates using `render_field` to apply Bootstrap classes and attributes.
Upgrade Django to 4.2+ or downgrade `django-widget-tweaks` to a version compatible with your Django installation.
For Jinja2, you must manually import and expose the desired functions (e.g., `render_field`, `add_class`) from `widget_tweaks.templatetags.widget_tweaks` into your Jinja2 environment's globals or filters. This requires custom setup in your Django project's Jinja2 configuration.
Ensure that 'widget_tweaks' is present in your `INSTALLED_APPS` list in `settings.py`.
If you need to define widget attributes or classes at the Python level (e.g., dynamically based on backend logic), use Django's built-in `widget.attrs` or form field initialization. `django-widget-tweaks` complements, but does not replace, Python-level form customization.