skip to navigation
skip to content

django-widget-tweaks 1.1.2

Tweak the form field rendering in templates, not in python-level form definitions.

Latest Version: 1.4.1

Tweak the form field rendering in templates, not in python-level form definitions. Altering CSS classes and HTML attributes is supported.

That should be enough for designers to customize field presentation (using CSS and unobtrusive javascript) without touching python code.

The license is MIT.


pip install django-widget-tweaks

Then add ‘widget_tweaks’ to INSTALLED_APPS.


This app provides several template filters that can alter CSS classes and HTML attributes of django form fields.


Adds or replaces any single html atribute for the form field.


{% load widget_tweaks %}

<!-- change input type (e.g. to HTML5) -->
{{ form.search_query|attr:"type:search" }}

<!-- add/change several attributes -->
{{ form.text|attr:"rows:20"|attr:"cols:20"|attr:"title:Hello, world!" }}

<!-- attributes without parameters -->
{{ form.search_query|attr:"autofocus" }}


Adds CSS class to field element. Split classes by whitespace in order to add several classes at once.


{% load widget_tweaks %}

<!-- add 2 extra css classes to field element -->
{{ form.title|add_class:"css_class_1 css_class_2" }}


Sets HTML5 data attribute ( ). Useful for unobtrusive javascript. It is just a shortcut for ‘attr’ filter that prepends attribute names with ‘data-‘ string.


{% load widget_tweaks %}

<!-- data-filters:"OverText" will be added to input field -->
{{ form.title|set_data:"filters:OverText" }}


Appends atribute value with extra data.


{% load widget_tweaks %}

<!-- add 2 extra css classes to field element -->
{{ form.title|append_attr:"class:css_class_1 css_class_2" }}

‘add_class’ filter is just a shortcut for ‘append_attr’ filter that adds values to the ‘class’ attribute.


The same as ‘add_class’ but adds css class only if validation failed for the field (field.errors is not empty).


{% load widget_tweaks %}

<!-- add 'error-border' css class on field error -->
{{ form.title|add_error_class:"error-border" }}


This is a template tag that can be used as an alternative to aforementioned filters. This template tag renders a field using a syntax similar to plain HTML attributes.


{% load widget_tweaks %}

<!-- change input type (e.g. to HTML5) -->
{% render_field form.search_query type="search" %}

<!-- add/change several attributes -->
{% render_field form.text rows="20" cols="20" title="Hello, world!" %}

<!-- append to an attribute -->
{% render_field form.title class+="css_class_1 css_class_2" %}

<!-- template variables can be used as attribute values -->
{% render_field form.text placeholder=form.text.label %}

field_type and widget_type

'field_type' and 'widget_type' are template filters that return field class name and field widget class name (in lower case).


{% load widget_tweaks %}

<div class="field {{ field|field_type }} {{ field|widget_type }} {{ field.html_name }}">
    {{ field }}


<div class="field charfield textinput name">
    <input id="id_name" type="text" name="name" maxlength="100" />

Filter chaining

The order django-widget-tweaks filters apply may seem counter-intuitive (leftmost filter wins):

{{ form.simple|attr:"foo:bar"|attr:"foo:baz" }}


<input foo="bar" type="text" name="simple" id="id_simple" />

It is not a bug, it is a feature that enables creating reusable templates with overridable defaults.

Reusable field template example:

{# inc/field.html #}
{% load widget_tweaks %}
<div>{{ field|attr:"foo:default_foo" }}</div>

Example usage:

{# my_template.html #}
{% load widget_tweaks %}
<form method='POST' action=''> {% csrf_token %}
    {% include "inc/field.html" with field=form.title %}
    {% include "inc/field.html" with field=form.description|attr:"foo:non_default_foo" %}

With ‘rightmost filter wins’ rule it wouldn’t be possible to override |attr:"foo:default_foo" in main template.


If you’ve found a bug, implemented a feature or have a suggestion, do not hesitate to contact me, fire an issue or send a pull request.

Source code:

Bug tracker:

running the tests

Make sure you have tox installed, then type


from the source checkout.


1.1.2 (2012-06-06)

  • support for template variables is added to render_field tag;
  • new field_type and widget_type filters.

1.1.1 (2012-03-22)

  • some issues with render_field tag are fixed.

1.1 (2012-03-22)

  • render_field template tag.

1.0 (2012-02-06)

  • filters return empty strings instead of raising exceptions if field is missing;
  • test running improvements;
  • python 3 support;
  • undocumented ‘behave’ filter is removed.

0.3 (2011-03-04)

  • add_error_class filter.

0.2.1 (2011-02-03)

  • Attributes customized in widgets are preserved;
  • no more extra whitespaces;
  • tests;

0.1 (2011-01-12)

Initial release.

File Type Py Version Uploaded on Size
django-widget-tweaks-1.1.2.tar.gz (md5) Source 2012-06-06 6KB