Custom CSS class in Form with template tag filter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# -*- coding:utf-8 -*-
from django import template

register = template.Library()


@register.filter
def cssclass(form, name_arg):
    """
    Replace the attribute css class for Field 'name' with 'arg'.
    name_args expects format: "attribute_name:special_class"
    """
    name, arg = name_arg.split(':')
    form.fields[name].widget.attrs['class'] = arg

    return form

More like this

  1. Custom CSS class in Form with template tag filter by kegan 3 years, 12 months ago
  2. BetterForm with fieldsets and row_attrs by carljm 4 years, 4 months ago
  3. Dynamically add css-classes to formfields by Lacour 1 year, 11 months ago
  4. Template Filter attr by marinho 5 years, 1 month ago
  5. Add CSS class template filter by lazerscience 2 years, 7 months ago

Comments

ws (on October 19, 2011):

This fuatere is in http://pypi.python.org/pypi/django-widget-tweaks

#

fernandogrd (on October 19, 2011):

ws, very interesting features in django-widget-tweaks, but it applies only to fields, this one applies for form itself.

#

(Forgotten your password?)