Form.auto_id
By default, the form rendering methods include:
- HTML
id
attributes on the form elements. - The corresponding
<label>
tags around the labels. An HTML<label>
tag designates which label text is associated with which form element. This small enhancement makes forms more usable and more accessible to assistive devices. It’s always a good idea to use<label>
tags.
The id
attribute values are generated by prepending id_
to the form field names. This behavior is configurable, though, if you want to change the id
convention or remove HTML id
attributes and <label>
tags entirely.
Use the auto_id
argument to the Form
constructor to control the id
and label behavior. This argument must be True
, False
or a string.
If auto_id
is False
, then the form output will not include <label>
tags nor id
attributes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | >>> f = ContactForm(auto_id = False ) >>> print (f.as_table()) <tr><th>Subject:< / th><td>< input type = "text" name = "subject" maxlength = "100" required / >< / td>< / tr> <tr><th>Message:< / th><td>< input type = "text" name = "message" required / >< / td>< / tr> <tr><th>Sender:< / th><td>< input type = "email" name = "sender" required / >< / td>< / tr> <tr><th>Cc myself:< / th><td>< input type = "checkbox" name = "cc_myself" / >< / td>< / tr> >>> print (f.as_ul()) <li>Subject: < input type = "text" name = "subject" maxlength = "100" required / >< / li> <li>Message: < input type = "text" name = "message" required / >< / li> <li>Sender: < input type = "email" name = "sender" required / >< / li> <li>Cc myself: < input type = "checkbox" name = "cc_myself" / >< / li> >>> print (f.as_p()) <p>Subject: < input type = "text" name = "subject" maxlength = "100" required / >< / p> <p>Message: < input type = "text" name = "message" required / >< / p> <p>Sender: < input type = "email" name = "sender" required / >< / p> <p>Cc myself: < input type = "checkbox" name = "cc_myself" / >< / p> |
If auto_id
is set to True
, then the form output will include <label>
tags and will simply use the field name as its id
for each form field:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | >>> f = ContactForm(auto_id = True ) >>> print (f.as_table()) <tr><th><label for = "subject" >Subject:< / label>< / th><td>< input id = "subject" type = "text" name = "subject" maxlength = "100" required / >< / td>< / tr> <tr><th><label for = "message" >Message:< / label>< / th><td>< input type = "text" name = "message" id = "message" required / >< / td>< / tr> <tr><th><label for = "sender" >Sender:< / label>< / th><td>< input type = "email" name = "sender" id = "sender" required / >< / td>< / tr> <tr><th><label for = "cc_myself" >Cc myself:< / label>< / th><td>< input type = "checkbox" name = "cc_myself" id = "cc_myself" / >< / td>< / tr> >>> print (f.as_ul()) <li><label for = "subject" >Subject:< / label> < input id = "subject" type = "text" name = "subject" maxlength = "100" required / >< / li> <li><label for = "message" >Message:< / label> < input type = "text" name = "message" id = "message" required / >< / li> <li><label for = "sender" >Sender:< / label> < input type = "email" name = "sender" id = "sender" required / >< / li> <li><label for = "cc_myself" >Cc myself:< / label> < input type = "checkbox" name = "cc_myself" id = "cc_myself" / >< / li> >>> print (f.as_p()) <p><label for = "subject" >Subject:< / label> < input id = "subject" type = "text" name = "subject" maxlength = "100" required / >< / p> <p><label for = "message" >Message:< / label> < input type = "text" name = "message" id = "message" required / >< / p> <p><label for = "sender" >Sender:< / label> < input type = "email" name = "sender" id = "sender" required / >< / p> <p><label for = "cc_myself" >Cc myself:< / label> < input type = "checkbox" name = "cc_myself" id = "cc_myself" / >< / p> |
If auto_id
is set to a string containing the format character '%s'
, then the form output will include <label>
tags, and will generate id
attributes based on the format string. For example, for a format string 'field_%s'
, a field named subject
will get the id
value 'field_subject'
. Continuing our example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | >>> f = ContactForm(auto_id = 'id_for_%s' ) >>> print (f.as_table()) <tr><th><label for = "id_for_subject" >Subject:< / label>< / th><td>< input id = "id_for_subject" type = "text" name = "subject" maxlength = "100" required / >< / td>< / tr> <tr><th><label for = "id_for_message" >Message:< / label>< / th><td>< input type = "text" name = "message" id = "id_for_message" required / >< / td>< / tr> <tr><th><label for = "id_for_sender" >Sender:< / label>< / th><td>< input type = "email" name = "sender" id = "id_for_sender" required / >< / td>< / tr> <tr><th><label for = "id_for_cc_myself" >Cc myself:< / label>< / th><td>< input type = "checkbox" name = "cc_myself" id = "id_for_cc_myself" / >< / td>< / tr> >>> print (f.as_ul()) <li><label for = "id_for_subject" >Subject:< / label> < input id = "id_for_subject" type = "text" name = "subject" maxlength = "100" required / >< / li> <li><label for = "id_for_message" >Message:< / label> < input type = "text" name = "message" id = "id_for_message" required / >< / li> <li><label for = "id_for_sender" >Sender:< / label> < input type = "email" name = "sender" id = "id_for_sender" required / >< / li> <li><label for = "id_for_cc_myself" >Cc myself:< / label> < input type = "checkbox" name = "cc_myself" id = "id_for_cc_myself" / >< / li> >>> print (f.as_p()) <p><label for = "id_for_subject" >Subject:< / label> < input id = "id_for_subject" type = "text" name = "subject" maxlength = "100" required / >< / p> <p><label for = "id_for_message" >Message:< / label> < input type = "text" name = "message" id = "id_for_message" required / >< / p> <p><label for = "id_for_sender" >Sender:< / label> < input type = "email" name = "sender" id = "id_for_sender" required / >< / p> <p><label for = "id_for_cc_myself" >Cc myself:< / label> < input type = "checkbox" name = "cc_myself" id = "id_for_cc_myself" / >< / p> |
If auto_id
is set to any other true value – such as a string that doesn’t include %s
– then the library will act as if auto_id
is True
.
By default, auto_id
is set to the string 'id_%s'
.
Please login to continue.