from django import template register = template.Library() @register.filter(name='addcss') def addcss(field, css): """ Adds the given class to the class attribute of the given field. E.g. adds the class 'bar' to the foo widget: {{ form.foo|addcss:"bar" }} """ return field.as_widget(attrs={"class": css})