| | 1029 | A widget can customize the 'for' value of its label by overriding id_for_label. |
| | 1030 | >>> class UserRegistration(Form): |
| | 1031 | ... class MyTextInput(TextInput): |
| | 1032 | ... def id_for_label(self, id): |
| | 1033 | ... return 'my_' + id |
| | 1034 | ... username = CharField(max_length=10, widget=MyTextInput) |
| | 1035 | ... password = CharField(widget=PasswordInput) |
| | 1036 | >>> p = UserRegistration() |
| | 1037 | >>> print p.as_ul() |
| | 1038 | <li><label for="my_id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></li> |
| | 1039 | <li><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></li> |
| | 1041 | If id_for_label returns None or is empty, the label won't get a 'for' attribute. |
| | 1042 | >>> class UserRegistration(Form): |
| | 1043 | ... class MyTextInput(TextInput): |
| | 1044 | ... def id_for_label(self, id): |
| | 1045 | ... return None |
| | 1046 | ... username = CharField(max_length=10, widget=MyTextInput) |
| | 1047 | ... password = CharField(widget=PasswordInput) |
| | 1048 | >>> p = UserRegistration() |
| | 1049 | >>> print p.as_ul() |
| | 1050 | <li><label>Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></li> |
| | 1051 | <li><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></li> |
| | 1052 | |