mirror of
https://github.com/django/django.git
synced 2025-06-13 07:29:13 +00:00
Updated documentation
This commit is contained in:
parent
bbd5a61896
commit
8654903fef
@ -111,6 +111,20 @@ with fields which are not based on choice -- such as a :class:`CharField` --
|
|||||||
but it is recommended to use a :class:`ChoiceField`-based field when the
|
but it is recommended to use a :class:`ChoiceField`-based field when the
|
||||||
choices are inherent to the model and not just the representational widget.
|
choices are inherent to the model and not just the representational widget.
|
||||||
|
|
||||||
|
.. versionchanged:: 5.2
|
||||||
|
|
||||||
|
For widgets that have options on their own, like those inheriting from ``ChoiceWidget``,
|
||||||
|
you can also pass option-specific attributes using ``option_attrs``.
|
||||||
|
For example, if you wish to set custom CSS classes for a RadioSelect's options, you could
|
||||||
|
do as follows:
|
||||||
|
|
||||||
|
.. code-block:: pycon
|
||||||
|
|
||||||
|
>>> from django.forms.widgets import RadioSelect
|
||||||
|
>>> widget = RadioSelect(choices=(("J", "John"),), option_attrs={"class": "special"})
|
||||||
|
>>> widget.render(name="beatle", value=["J"])
|
||||||
|
'<div><div>\n <label><input type="radio" name="beatle" value="J" class="special" checked>\n John</label>\n\n</div>\n</div>'
|
||||||
|
|
||||||
Customizing widget instances
|
Customizing widget instances
|
||||||
============================
|
============================
|
||||||
|
|
||||||
@ -195,18 +209,6 @@ Django will then include the extra attributes in the rendered output:
|
|||||||
You can also set the HTML ``id`` using :attr:`~Widget.attrs`. See
|
You can also set the HTML ``id`` using :attr:`~Widget.attrs`. See
|
||||||
:attr:`BoundField.id_for_label` for an example.
|
:attr:`BoundField.id_for_label` for an example.
|
||||||
|
|
||||||
For widgets that have options on their own, like those inheriting from ``ChoiceWidget``,
|
|
||||||
you can also pass option-specific attributes using ``option_attrs``.
|
|
||||||
For example, if you wish to set custom CSS classes for a RadioSelect's options, you could
|
|
||||||
do as follows:
|
|
||||||
|
|
||||||
.. code-block:: pycon
|
|
||||||
|
|
||||||
>>> from django.forms.widgets import RadioSelect
|
|
||||||
>>> widget = RadioSelect(choices=(("J", "John"),), option_attrs={"class": "special"})
|
|
||||||
>>> widget.render(name="beatle", value=["J"])
|
|
||||||
'<div><div>\n <label><input type="radio" name="beatle" value="J" class="special" checked>\n John</label>\n\n</div>\n</div>'
|
|
||||||
|
|
||||||
.. _styling-widget-classes:
|
.. _styling-widget-classes:
|
||||||
|
|
||||||
Styling widget classes
|
Styling widget classes
|
||||||
@ -757,6 +759,20 @@ that specifies the template used to render each choice. For example, for the
|
|||||||
``choices`` attribute. If it does, it will override anything you set
|
``choices`` attribute. If it does, it will override anything you set
|
||||||
here when the attribute is updated on the :class:`Field`.
|
here when the attribute is updated on the :class:`Field`.
|
||||||
|
|
||||||
|
.. attribute:: Select.option_attrs
|
||||||
|
|
||||||
|
.. versionadded:: 5.2
|
||||||
|
|
||||||
|
An optional dictionary containing option-specific HTML attributes to
|
||||||
|
be set on the rendered widget.
|
||||||
|
|
||||||
|
.. code-block:: pycon
|
||||||
|
|
||||||
|
>>> from django.forms.widgets import RadioSelect
|
||||||
|
>>> widget = RadioSelect(choices=(("J", "John"),), option_attrs={"class": "special"})
|
||||||
|
>>> widget.render(name="beatle", value=["J"])
|
||||||
|
'<div><div>\n <label><input type="radio" name="beatle" value="J" class="special" checked>\n John</label>\n\n</div>\n</div>'
|
||||||
|
|
||||||
``NullBooleanSelect``
|
``NullBooleanSelect``
|
||||||
~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
@ -223,6 +223,9 @@ Forms
|
|||||||
* The new :class:`~django.forms.TelInput` form widget is for entering telephone
|
* The new :class:`~django.forms.TelInput` form widget is for entering telephone
|
||||||
numbers and renders as ``<input type="tel" ...>``.
|
numbers and renders as ``<input type="tel" ...>``.
|
||||||
|
|
||||||
|
* The new :attr:`~django.forms.Select.option_attrs` attribute can be used to
|
||||||
|
specify option-specific HTML attributes.
|
||||||
|
|
||||||
Generic Views
|
Generic Views
|
||||||
~~~~~~~~~~~~~
|
~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user