Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #20567 - Documented BoundField.id_for_label.

Thanks littlepig for the suggestion.
  • Loading branch information...
commit 175a102ddceae2ebd1a4e03916a23adafc35bda5 1 parent a47116c
Tim Graham authored
23  docs/ref/forms/api.txt
@@ -688,6 +688,29 @@ by a ``Widget``::
688 688
     >>> print(bound_form['subject'].value())
689 689
     hi
690 690
 
  691
+.. attribute:: BoundField.id_for_label
  692
+
  693
+Use this property to render the ID of this field. For example, if you are
  694
+manually constructing a ``<label>`` in your template (despite the fact that
  695
+:meth:`~BoundField.label_tag` will do this for you):
  696
+
  697
+.. code-block:: html+django
  698
+
  699
+    <label for="{{ form.my_field.id_for_label }}">...</label>{{ my_field }}
  700
+
  701
+By default, this will be the field's name prefixed by ``id_``
  702
+("``id_my_field``" for the example above). You may modify the ID by setting
  703
+:attr:`~django.forms.Widget.attrs` on the field's widget. For example,
  704
+declaring a field like this::
  705
+
  706
+    my_field = forms.CharField(widget=forms.TextInput(attrs={'id': 'myFIELD'}))
  707
+
  708
+and using the template above, would render something like:
  709
+
  710
+.. code-block:: html
  711
+
  712
+    <label for="myFIELD">...</label><input id="myFIELD" type="text" name="my_field" />
  713
+
691 714
 .. _binding-uploaded-files:
692 715
 
693 716
 Binding uploaded files to a form
5  docs/ref/forms/widgets.txt
@@ -163,6 +163,9 @@ Django will then include the extra attributes in the rendered output:
163 163
     <tr><th>Url:</th><td><input type="url" name="url"/></td></tr>
164 164
     <tr><th>Comment:</th><td><input type="text" name="comment" size="40"/></td></tr>
165 165
 
  166
+You can also set the HTML ``id`` using :attr:`~Widget.attrs`. See
  167
+:attr:`BoundField.id_for_label` for an example.
  168
+
166 169
 .. _styling-widget-classes:
167 170
 
168 171
 Styling widget classes
@@ -251,7 +254,7 @@ foundation for custom widgets.
251 254
         into two separate values::
252 255
 
253 256
             from django.forms import MultiWidget
254  
-            
  257
+
255 258
             class SplitDateTimeWidget(MultiWidget):
256 259
 
257 260
                 # ...

0 notes on commit 175a102

Please sign in to comment.
Something went wrong with that request. Please try again.