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 and I have tested it in the shell and I can do something like:
for field in form:
print field.label_tag(attrs{'class':'Foo'})
I will see the class='Foo'
in my output, but I don't see a way to add an attrs
argument from the template - in fact, templates are designed specifically against that, no?
Is there a way in my form definition to define the class to be displayed in the label?
In the form, I can do the following to give the inputs a class
self.fields['some_field'].widget.attrs['class'] = 'Foo'
I just need to have it output the class for the <label />
as well.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…