I need some way to add a class attribute to the output of the label_tag() method for a forms field.
I see that there is the ability to pass in an attrs dictionary a
Technique 1
I take issue with another answer's assertion that a filter would be "less elegant." As you can see, it's very elegant indeed.
@register.filter(is_safe=True)
def label_with_classes(value, arg):
return value.label_tag(attrs={'class': arg})
Using this in a template is just as elegant:
{{ form.my_field|label_with_classes:"class1 class2"}}
Technique 2
Alternatively, one of the more interesting technique I've found is: Adding * to required fields.
You create a decorator for BoundField.label_tag that will call it with attrs set appropriately. Then you monkey patch BoundField so that calling BoundField.label_tag calls the decorated function.
from django.forms.forms import BoundField
def add_control_label(f):
def control_label_tag(self, contents=None, attrs=None):
if attrs is None: attrs = {}
attrs['class'] = 'control-label'
return f(self, contents, attrs)
return control_label_tag
BoundField.label_tag = add_control_label(BoundField.label_tag)
A custom template tag seems to be the solution. A custom filter would also do, although it can be less elegant. But you would need to fall back to custom form rendering in both cases.
If this is a task of high importance; I'd create a Mixin that allows me to annotate the form fields with label classes and supplies form rendering methods using those classes. So that the following code works:
{{ form.as_table_with_label_classes }}
But I'd like to ask; Do you really need a class on the label tag? I mean HTML design-wise. It is absolutely necessary to add a class in there? Couldn't it be solved with some CSS like:
encapsulating_selector label {
some-attr: some-value;
}
I sometimes use jQuery for such cases where; it will improve the page if it works, but it won't be a disaster if it doesn't. And keep the HTML source as lean as possible.