Skip to content

Instantly share code, notes, and snippets.

@samkit5495
Created June 15, 2017 19:10
Show Gist options
  • Save samkit5495/0cb2d2216e57badf33504e32952649ae to your computer and use it in GitHub Desktop.
Save samkit5495/0cb2d2216e57badf33504e32952649ae to your computer and use it in GitHub Desktop.
Add attributes to django forms with templatetags
from django import template
register = template.Library()
@register.filter(name='addattr')
def addattr(field, attr):
attrs={}
attr_list = [i.strip() for i in attr.split('|')]
for i in attr_list:
attribute = [j.strip() for j in i.split(':')]
attrs[attribute[0]]=attribute[1]
return field.as_widget(attrs=attrs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment