1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import re
from django import template
register = template.Library()
CLASS_PATTERN = re.compile(r'\bclass="[\w\d]*"')
def cssclass(value, arg):
"""
Replace the attribute css class for Field 'value' with 'arg'.
"""
attrs = value.field.widget.attrs
orig = attrs['class'] if 'class' in attrs else None
attrs['class'] = arg
rendered = str(value)
if orig:
attrs['class']
else:
del attrs['class']
return rendered
register.filter('cssclass', cssclass)
|
More like this
- Custom CSS class in Form with template tag filter by fernandogrd 1 year, 7 months ago
- BetterForm with fieldsets and row_attrs by carljm 4 years, 3 months ago
- Honeypot Field by SmileyChris 6 years, 1 month ago
- Dynamically add css-classes to formfields by Lacour 1 year, 10 months ago
- Template Filter attr by marinho 5 years ago
Comments