summaryrefslogtreecommitdiff
path: root/docs/ref/forms
diff options
context:
space:
mode:
authorMoritz Sichert <moritz.sichert@googlemail.com>2015-08-10 13:23:24 +0200
committerTim Graham <timograham@gmail.com>2015-09-21 08:42:14 -0400
commit2f53d342f1ce2fb36cddbd90e24c4fda86389c27 (patch)
tree2cbc9ba4553d63a42bb5d8422f3a06a649f0bdca /docs/ref/forms
parent534aaf56f4a8e261e111426b2a709e2f8816192f (diff)
Fixed #12856 -- Documented BoundField API.
Diffstat (limited to 'docs/ref/forms')
-rw-r--r--docs/ref/forms/api.txt225
-rw-r--r--docs/ref/forms/widgets.txt10
2 files changed, 160 insertions, 75 deletions
diff --git a/docs/ref/forms/api.txt b/docs/ref/forms/api.txt
index ca9e4d868f..a8d2fa77ff 100644
--- a/docs/ref/forms/api.txt
+++ b/docs/ref/forms/api.txt
@@ -767,6 +767,8 @@ method you're using::
<p>Sender: <input type="email" name="sender" value="invalid email address" /></p>
<p>Cc myself: <input checked="checked" type="checkbox" name="cc_myself" /></p>
+.. _ref-forms-error-list-format:
+
Customizing the error list format
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -792,10 +794,10 @@ Python 2)::
<p>Cc myself: <input checked="checked" type="checkbox" name="cc_myself" /></p>
More granular output
-~~~~~~~~~~~~~~~~~~~~
+--------------------
-The ``as_p()``, ``as_ul()`` and ``as_table()`` methods are simply shortcuts for
-lazy developers -- they're not the only way a form object can be displayed.
+The ``as_p()``, ``as_ul()``, and ``as_table()`` methods are simply shortcuts --
+they're not the only way a form object can be displayed.
.. class:: BoundField
@@ -830,107 +832,180 @@ The field-specific output honors the form object's ``auto_id`` setting::
>>> print(f['message'])
<input type="text" name="message" id="id_message" />
-For a field's list of errors, access the field's ``errors`` attribute.
+Attributes of ``BoundField``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. attribute:: BoundField.data
+
+ This property returns the data for this :class:`~django.forms.BoundField`
+ extracted by the widget's :meth:`~django.forms.Widget.value_from_datadict`
+ method, or ``None`` if it wasn't given::
+
+ >>> unbound_form = ContactForm()
+ >>> print(unbound_form['subject'].data)
+ None
+ >>> bound_form = ContactForm(data={'subject': 'My Subject'})
+ >>> print(bound_form['subject'].data)
+ My Subject
.. attribute:: BoundField.errors
-A list-like object that is displayed as an HTML ``<ul class="errorlist">``
-when printed::
+ A :ref:`list-like object <ref-forms-error-list-format>` that is displayed
+ as an HTML ``<ul class="errorlist">`` when printed::
- >>> data = {'subject': 'hi', 'message': '', 'sender': '', 'cc_myself': ''}
- >>> f = ContactForm(data, auto_id=False)
- >>> print(f['message'])
- <input type="text" name="message" />
- >>> f['message'].errors
- ['This field is required.']
- >>> print(f['message'].errors)
- <ul class="errorlist"><li>This field is required.</li></ul>
- >>> f['subject'].errors
- []
- >>> print(f['subject'].errors)
+ >>> data = {'subject': 'hi', 'message': '', 'sender': '', 'cc_myself': ''}
+ >>> f = ContactForm(data, auto_id=False)
+ >>> print(f['message'])
+ <input type="text" name="message" />
+ >>> f['message'].errors
+ ['This field is required.']
+ >>> print(f['message'].errors)
+ <ul class="errorlist"><li>This field is required.</li></ul>
+ >>> f['subject'].errors
+ []
+ >>> print(f['subject'].errors)
- >>> str(f['subject'].errors)
- ''
+ >>> str(f['subject'].errors)
+ ''
-.. method:: BoundField.label_tag(contents=None, attrs=None, label_suffix=None)
+.. attribute:: BoundField.field
-To separately render the label tag of a form field, you can call its
-``label_tag`` method::
+ The form :class:`~django.forms.Field` instance from the form class that
+ this :class:`~django.forms.BoundField` wraps.
- >>> f = ContactForm(data)
- >>> print(f['message'].label_tag())
- <label for="id_message">Message:</label>
+.. attribute:: BoundField.form
-Optionally, you can provide the ``contents`` parameter which will replace the
-auto-generated label tag. An optional ``attrs`` dictionary may contain
-additional attributes for the ``<label>`` tag.
+ The :class:`~django.forms.Form` instance this :class:`~django.forms.BoundField`
+ is bound to.
-The HTML that's generated includes the form's
-:attr:`~django.forms.Form.label_suffix` (a colon, by default) or, if set, the
-current field's :attr:`~django.forms.Field.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``.
+.. attribute:: BoundField.help_text
-.. versionchanged:: 1.8
+ The :attr:`~django.forms.Field.help_text` of the field.
- The label includes :attr:`~Form.required_css_class` if applicable.
+.. attribute:: BoundField.html_name
-.. method:: BoundField.css_classes()
+ The name that will be used in the widget's HTML ``name`` attribute. It takes
+ the form :attr:`~django.forms.Form.prefix` into account.
-When you use Django's rendering shortcuts, CSS classes are used to
-indicate required form fields or fields that contain errors. If you're
-manually rendering a form, you can access these CSS classes using the
-``css_classes`` method::
+.. attribute:: BoundField.id_for_label
- >>> f = ContactForm(data)
- >>> f['message'].css_classes()
- 'required'
+ Use this property to render the ID of this field. For example, if you are
+ manually constructing a ``<label>`` in your template (despite the fact that
+ :meth:`~BoundField.label_tag` will do this for you):
-If you want to provide some additional classes in addition to the
-error and required classes that may be required, you can provide
-those classes as an argument::
+ .. code-block:: html+django
- >>> f = ContactForm(data)
- >>> f['message'].css_classes('foo bar')
- 'foo bar required'
+ <label for="{{ form.my_field.id_for_label }}">...</label>{{ my_field }}
-.. method:: BoundField.value()
+ By default, this will be the field's name prefixed by ``id_``
+ ("``id_my_field``" for the example above). You may modify the ID by setting
+ :attr:`~django.forms.Widget.attrs` on the field's widget. For example,
+ declaring a field like this::
-Use this method to render the raw value of this field as it would be rendered
-by a ``Widget``::
+ my_field = forms.CharField(widget=forms.TextInput(attrs={'id': 'myFIELD'}))
- >>> initial = {'subject': 'welcome'}
- >>> unbound_form = ContactForm(initial=initial)
- >>> bound_form = ContactForm(data, initial=initial)
- >>> print(unbound_form['subject'].value())
- welcome
- >>> print(bound_form['subject'].value())
- hi
+ and using the template above, would render something like:
-.. attribute:: BoundField.id_for_label
+ .. code-block:: html
+
+ <label for="myFIELD">...</label><input id="myFIELD" type="text" name="my_field" />
-Use this property to render the ID of this field. For example, if you are
-manually constructing a ``<label>`` in your template (despite the fact that
-:meth:`~BoundField.label_tag` will do this for you):
+.. attribute:: BoundField.is_hidden
-.. code-block:: html+django
+ Returns ``True`` if this :class:`~django.forms.BoundField`'s widget is
+ hidden.
- <label for="{{ form.my_field.id_for_label }}">...</label>{{ my_field }}
+.. attribute:: BoundField.label
-By default, this will be the field's name prefixed by ``id_``
-("``id_my_field``" for the example above). You may modify the ID by setting
-:attr:`~django.forms.Widget.attrs` on the field's widget. For example,
-declaring a field like this::
+ The :attr:`~django.forms.Field.label` of the field. This is used in
+ :meth:`~BoundField.label_tag`.
+
+.. attribute:: BoundField.name
+
+ The name of this field in the form::
+
+ >>> f = ContactForm()
+ >>> print(f['subject'].name)
+ subject
+ >>> print(f['message'].name)
+ message
+
+Methods of ``BoundField``
+~~~~~~~~~~~~~~~~~~~~~~~~~
- my_field = forms.CharField(widget=forms.TextInput(attrs={'id': 'myFIELD'}))
+.. method:: BoundField.as_hidden(attrs=None, **kwargs)
-and using the template above, would render something like:
+ Returns a string of HTML for representing this as an ``<input type="hidden">``.
+
+ ``**kwargs`` are passed to :meth:`~django.forms.BoundField.as_widget`.
+
+ This method is primarily used internally. You should use a widget instead.
+
+.. method:: BoundField.as_widget(widget=None, attrs=None, only_initial=False)
+
+ Renders the field by rendering the passed widget, adding any HTML
+ attributes passed as ``attrs``. If no widget is specified, then the
+ field's default widget will be used.
+
+ ``only_initial`` is used by Django internals and should not be set
+ explicitly.
+
+.. method:: BoundField.css_classes()
+
+ When you use Django's rendering shortcuts, CSS classes are used to
+ indicate required form fields or fields that contain errors. If you're
+ manually rendering a form, you can access these CSS classes using the
+ ``css_classes`` method::
+
+ >>> f = ContactForm(data={'message': ''})
+ >>> f['message'].css_classes()
+ 'required'
+
+ If you want to provide some additional classes in addition to the
+ error and required classes that may be required, you can provide
+ those classes as an argument::
+
+ >>> f = ContactForm(data={'message': ''})
+ >>> f['message'].css_classes('foo bar')
+ 'foo bar required'
+
+.. method:: BoundField.label_tag(contents=None, attrs=None, label_suffix=None)
+
+ 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
+ :attr:`~django.forms.Form.label_suffix` (a colon, by default) or, if set, the
+ current field's :attr:`~django.forms.Field.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``.
+
+ .. versionchanged:: 1.8
+
+ The label includes :attr:`~Form.required_css_class` if applicable.
+
+.. method:: BoundField.value()
-.. code-block:: html
+ Use this method to render the raw value of this field as it would be rendered
+ by a ``Widget``::
- <label for="myFIELD">...</label><input id="myFIELD" type="text" name="my_field" />
+ >>> initial = {'subject': 'welcome'}
+ >>> unbound_form = ContactForm(initial=initial)
+ >>> bound_form = ContactForm(data={'subject': 'hi'}, initial=initial)
+ >>> print(unbound_form['subject'].value())
+ welcome
+ >>> print(bound_form['subject'].value())
+ hi
Customizing ``BoundField``
--------------------------
diff --git a/docs/ref/forms/widgets.txt b/docs/ref/forms/widgets.txt
index ab7679bf34..5e51ec10b1 100644
--- a/docs/ref/forms/widgets.txt
+++ b/docs/ref/forms/widgets.txt
@@ -230,6 +230,16 @@ foundation for custom widgets.
In older versions, this attribute was only defined on the date
and time widgets (as ``False``).
+ .. method:: id_for_label(self, id_)
+
+ Returns the HTML ID attribute of this widget for use by a ``<label>``,
+ given the ID of the field. Returns ``None`` if an ID isn't available.
+
+ This hook is necessary because some widgets have multiple HTML
+ elements and, thus, multiple IDs. In that case, this method should
+ return an ID value that corresponds to the first ID in the widget's
+ tags.
+
.. method:: render(name, value, attrs=None)
Returns HTML for the widget, as a Unicode string. This method must be