added addclass filter

This commit is contained in:
Nicholas Matlaga 2017-07-13 17:36:03 -04:00 committed by Griatch
parent 570762f1fd
commit 0a4102fa54
2 changed files with 16 additions and 0 deletions

View file

@ -0,0 +1,16 @@
from django import template
register = template.Library()
@register.filter(name='addclass')
def addclass(field, given_class):
existing_classes = field.field.widget.attrs.get('class', None)
if existing_classes:
if existing_classes.find(given_class) == -1:
# if the given class doesn't exist in the existing classes
classes = existing_classes + ' ' + given_class
else:
classes = existing_classes
else:
classes = given_class
return field.as_widget(attrs={"class": classes})