Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Django template filter to add attributes to form fields
# From
from django import template
register = template.Library()
def add_attributes(field, css):
attrs = {}
definition = css.split(',')
for d in definition:
if ':' not in d:
attrs['class'] = d
t, v = d.split(':')
attrs[t] = v
return field.as_widget(attrs=attrs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment