forms.BoundField.label_tag()

BoundField.label_tag(contents=None, attrs=None, label_suffix=None) [source]

To separately render the label tag of a form field, you can call its label_tag() method:

>>> f = ContactForm(data={'message': ''})
>>> print(f['message'].label_tag())
<label for="id_message">Message:</label>

You can provide the contents parameter which will replace the auto-generated label tag. An attrs dictionary may contain additional attributes for the <label> tag.

The HTML that’s generated includes the form’s label_suffix (a colon, by default) or, if set, the current field’s label_suffix. The optional label_suffix parameter allows you to override any previously set suffix. For example, you can use an empty string to hide the label on selected fields. If you need to do this in a template, you could write a custom filter to allow passing parameters to label_tag.

doc_Django
2016-10-09 18:36:39
Comments
Leave a Comment

Please login to continue.