2008-08-24 06:25:40 +08:00
|
|
|
=======
|
|
|
|
Widgets
|
|
|
|
=======
|
|
|
|
|
|
|
|
.. module:: django.forms.widgets
|
|
|
|
:synopsis: Django's built-in form widgets.
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. currentmodule:: django.forms
|
|
|
|
|
2015-05-26 22:27:55 +08:00
|
|
|
A widget is Django's representation of an HTML input element. The widget
|
2008-08-24 06:25:40 +08:00
|
|
|
handles the rendering of the HTML, and the extraction of data from a GET/POST
|
|
|
|
dictionary that corresponds to the widget.
|
|
|
|
|
2016-07-22 13:05:17 +08:00
|
|
|
The HTML generated by the built-in widgets uses HTML5 syntax, targeting
|
|
|
|
``<!DOCTYPE html>``. For example, it uses boolean attributes such as ``checked``
|
|
|
|
rather than the XHTML style of ``checked='checked'``.
|
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
.. tip::
|
|
|
|
|
|
|
|
Widgets should not be confused with the :doc:`form fields </ref/forms/fields>`.
|
|
|
|
Form fields deal with the logic of input validation and are used directly
|
|
|
|
in templates. Widgets deal with rendering of HTML form input elements on
|
|
|
|
the web page and extraction of raw submitted data. However, widgets do
|
|
|
|
need to be :ref:`assigned <widget-to-field>` to form fields.
|
|
|
|
|
|
|
|
.. _widget-to-field:
|
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
Specifying widgets
|
2016-01-03 18:56:22 +08:00
|
|
|
==================
|
2011-06-16 23:27:19 +08:00
|
|
|
|
|
|
|
Whenever you specify a field on a form, Django will use a default widget
|
|
|
|
that is appropriate to the type of data that is to be displayed. To find
|
|
|
|
which widget is used on which field, see the documentation about
|
2014-08-04 19:29:59 +08:00
|
|
|
:ref:`built-in-fields`.
|
2011-06-16 23:27:19 +08:00
|
|
|
|
|
|
|
However, if you want to use a different widget for a field, you can
|
2019-06-17 22:54:55 +08:00
|
|
|
use the :attr:`~Field.widget` argument on the field definition. For example::
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
from django import forms
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
class CommentForm(forms.Form):
|
|
|
|
name = forms.CharField()
|
|
|
|
url = forms.URLField()
|
|
|
|
comment = forms.CharField(widget=forms.Textarea)
|
2011-06-16 23:27:19 +08:00
|
|
|
|
|
|
|
This would specify a form with a comment that uses a larger :class:`Textarea`
|
|
|
|
widget, rather than the default :class:`TextInput` widget.
|
|
|
|
|
|
|
|
Setting arguments for widgets
|
2016-01-03 18:56:22 +08:00
|
|
|
=============================
|
2011-06-16 23:27:19 +08:00
|
|
|
|
|
|
|
Many widgets have optional extra arguments; they can be set when defining the
|
|
|
|
widget on the field. In the following example, the
|
2015-01-26 11:28:57 +08:00
|
|
|
:attr:`~django.forms.SelectDateWidget.years` attribute is set for a
|
|
|
|
:class:`~django.forms.SelectDateWidget`::
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2013-07-28 18:58:19 +08:00
|
|
|
from django import forms
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2019-05-15 20:31:42 +08:00
|
|
|
BIRTH_YEAR_CHOICES = ['1980', '1981', '1982']
|
|
|
|
FAVORITE_COLORS_CHOICES = [
|
2015-06-11 21:34:03 +08:00
|
|
|
('blue', 'Blue'),
|
|
|
|
('green', 'Green'),
|
|
|
|
('black', 'Black'),
|
2019-05-15 20:31:42 +08:00
|
|
|
]
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
class SimpleForm(forms.Form):
|
2015-01-26 11:28:57 +08:00
|
|
|
birth_year = forms.DateField(widget=forms.SelectDateWidget(years=BIRTH_YEAR_CHOICES))
|
2016-06-03 03:56:13 +08:00
|
|
|
favorite_colors = forms.MultipleChoiceField(
|
|
|
|
required=False,
|
|
|
|
widget=forms.CheckboxSelectMultiple,
|
|
|
|
choices=FAVORITE_COLORS_CHOICES,
|
|
|
|
)
|
2011-06-16 23:27:19 +08:00
|
|
|
|
|
|
|
See the :ref:`built-in widgets` for more information about which widgets
|
|
|
|
are available and which arguments they accept.
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
Widgets inheriting from the ``Select`` widget
|
|
|
|
=============================================
|
2011-06-16 23:27:19 +08:00
|
|
|
|
|
|
|
Widgets inheriting from the :class:`Select` widget deal with choices. They
|
|
|
|
present the user with a list of options to choose from. The different widgets
|
|
|
|
present this choice differently; the :class:`Select` widget itself uses a
|
|
|
|
``<select>`` HTML list representation, while :class:`RadioSelect` uses radio
|
|
|
|
buttons.
|
|
|
|
|
|
|
|
:class:`Select` widgets are used by default on :class:`ChoiceField` fields. The
|
|
|
|
choices displayed on the widget are inherited from the :class:`ChoiceField` and
|
|
|
|
changing :attr:`ChoiceField.choices` will update :attr:`Select.choices`. For
|
2011-10-14 08:12:01 +08:00
|
|
|
example::
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
>>> from django import forms
|
2019-05-15 20:31:42 +08:00
|
|
|
>>> CHOICES = [('1', 'First'), ('2', 'Second')]
|
2011-10-14 08:12:01 +08:00
|
|
|
>>> choice_field = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES)
|
|
|
|
>>> choice_field.choices
|
|
|
|
[('1', 'First'), ('2', 'Second')]
|
|
|
|
>>> choice_field.widget.choices
|
|
|
|
[('1', 'First'), ('2', 'Second')]
|
2019-05-15 20:31:42 +08:00
|
|
|
>>> choice_field.widget.choices = []
|
|
|
|
>>> choice_field.choices = [('1', 'First and only')]
|
2011-10-14 08:12:01 +08:00
|
|
|
>>> choice_field.widget.choices
|
|
|
|
[('1', 'First and only')]
|
2011-06-16 23:27:19 +08:00
|
|
|
|
|
|
|
Widgets which offer a :attr:`~Select.choices` attribute can however be used
|
|
|
|
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
|
|
|
|
choices are inherent to the model and not just the representational widget.
|
|
|
|
|
|
|
|
Customizing widget instances
|
2016-01-03 18:56:22 +08:00
|
|
|
============================
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
When Django renders a widget as HTML, it only renders very minimal markup -
|
|
|
|
Django doesn't add class names, or any other widget-specific attributes. This
|
|
|
|
means, for example, that all :class:`TextInput` widgets will appear the same
|
|
|
|
on your Web pages.
|
|
|
|
|
|
|
|
There are two ways to customize widgets: :ref:`per widget instance
|
|
|
|
<styling-widget-instances>` and :ref:`per widget class <styling-widget-classes>`.
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
.. _styling-widget-instances:
|
|
|
|
|
|
|
|
Styling widget instances
|
2016-01-03 18:56:22 +08:00
|
|
|
------------------------
|
2012-09-12 18:59:19 +08:00
|
|
|
|
|
|
|
If you want to make one widget instance look different from another, you will
|
|
|
|
need to specify additional attributes at the time when the widget object is
|
|
|
|
instantiated and assigned to a form field (and perhaps add some rules to your
|
|
|
|
CSS files).
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2019-06-17 22:54:55 +08:00
|
|
|
For example, take the following form::
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
from django import forms
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
class CommentForm(forms.Form):
|
|
|
|
name = forms.CharField()
|
|
|
|
url = forms.URLField()
|
|
|
|
comment = forms.CharField()
|
2011-06-16 23:27:19 +08:00
|
|
|
|
|
|
|
This form will include three default :class:`TextInput` widgets, with default
|
|
|
|
rendering -- no CSS class, no extra attributes. This means that the input boxes
|
2011-10-14 08:12:01 +08:00
|
|
|
provided for each widget will be rendered exactly the same::
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
>>> f = CommentForm(auto_id=False)
|
|
|
|
>>> f.as_table()
|
2018-01-21 15:09:10 +08:00
|
|
|
<tr><th>Name:</th><td><input type="text" name="name" required></td></tr>
|
|
|
|
<tr><th>Url:</th><td><input type="url" name="url" required></td></tr>
|
|
|
|
<tr><th>Comment:</th><td><input type="text" name="comment" required></td></tr>
|
2011-06-16 23:27:19 +08:00
|
|
|
|
|
|
|
On a real Web page, you probably don't want every widget to look the same. You
|
|
|
|
might want a larger input element for the comment, and you might want the
|
2012-09-11 01:21:29 +08:00
|
|
|
'name' widget to have some special CSS class. It is also possible to specify
|
|
|
|
the 'type' attribute to take advantage of the new HTML5 input types. To do
|
2012-09-12 18:59:19 +08:00
|
|
|
this, you use the :attr:`Widget.attrs` argument when creating the widget::
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
class CommentForm(forms.Form):
|
2014-02-04 19:24:13 +08:00
|
|
|
name = forms.CharField(widget=forms.TextInput(attrs={'class': 'special'}))
|
2011-10-14 08:12:01 +08:00
|
|
|
url = forms.URLField()
|
2014-02-04 19:24:13 +08:00
|
|
|
comment = forms.CharField(widget=forms.TextInput(attrs={'size': '40'}))
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2018-03-08 05:58:55 +08:00
|
|
|
You can also modify a widget in the form definition::
|
|
|
|
|
|
|
|
class CommentForm(forms.Form):
|
|
|
|
name = forms.CharField()
|
|
|
|
url = forms.URLField()
|
|
|
|
comment = forms.CharField()
|
|
|
|
|
|
|
|
name.widget.attrs.update({'class': 'special'})
|
|
|
|
comment.widget.attrs.update(size='40')
|
|
|
|
|
|
|
|
Or if the field isn't declared directly on the form (such as model form fields),
|
|
|
|
you can use the :attr:`Form.fields` attribute::
|
|
|
|
|
|
|
|
class CommentForm(forms.ModelForm):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields['name'].widget.attrs.update({'class': 'special'})
|
|
|
|
self.fields['comment'].widget.attrs.update(size='40')
|
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
Django will then include the extra attributes in the rendered output:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
>>> f = CommentForm(auto_id=False)
|
|
|
|
>>> f.as_table()
|
2018-01-21 15:09:10 +08:00
|
|
|
<tr><th>Name:</th><td><input type="text" name="name" class="special" required></td></tr>
|
|
|
|
<tr><th>Url:</th><td><input type="url" name="url" required></td></tr>
|
|
|
|
<tr><th>Comment:</th><td><input type="text" name="comment" size="40" required></td></tr>
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2013-06-07 02:15:26 +08:00
|
|
|
You can also set the HTML ``id`` using :attr:`~Widget.attrs`. See
|
|
|
|
:attr:`BoundField.id_for_label` for an example.
|
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
.. _styling-widget-classes:
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
Styling widget classes
|
2016-01-03 18:56:22 +08:00
|
|
|
----------------------
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
With widgets, it is possible to add assets (``css`` and ``javascript``)
|
2012-09-12 18:59:19 +08:00
|
|
|
and more deeply customize their appearance and behavior.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
In a nutshell, you will need to subclass the widget and either
|
2013-06-20 14:51:20 +08:00
|
|
|
:ref:`define a "Media" inner class <assets-as-a-static-definition>` or
|
|
|
|
:ref:`create a "media" property <dynamic-property>`.
|
2012-09-12 18:59:19 +08:00
|
|
|
|
|
|
|
These methods involve somewhat advanced Python programming and are described in
|
2013-06-20 14:51:20 +08:00
|
|
|
detail in the :doc:`Form Assets </topics/forms/media>` topic guide.
|
2012-09-12 18:59:19 +08:00
|
|
|
|
|
|
|
.. _base-widget-classes:
|
|
|
|
|
2016-01-03 18:56:22 +08:00
|
|
|
Base widget classes
|
|
|
|
===================
|
2011-08-21 19:51:48 +08:00
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
Base widget classes :class:`Widget` and :class:`MultiWidget` are subclassed by
|
|
|
|
all the :ref:`built-in widgets <built-in widgets>` and may serve as a
|
|
|
|
foundation for custom widgets.
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2016-01-03 18:56:22 +08:00
|
|
|
``Widget``
|
|
|
|
----------
|
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
.. class:: Widget(attrs=None)
|
|
|
|
|
|
|
|
This abstract class cannot be rendered, but provides the basic attribute
|
|
|
|
:attr:`~Widget.attrs`. You may also implement or override the
|
|
|
|
:meth:`~Widget.render()` method on custom widgets.
|
2011-06-16 23:27:19 +08:00
|
|
|
|
|
|
|
.. attribute:: Widget.attrs
|
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
A dictionary containing HTML attributes to be set on the rendered
|
|
|
|
widget.
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2014-08-18 22:30:44 +08:00
|
|
|
.. code-block:: pycon
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2013-05-19 17:15:35 +08:00
|
|
|
>>> from django import forms
|
2018-03-12 21:39:09 +08:00
|
|
|
>>> name = forms.TextInput(attrs={'size': 10, 'title': 'Your name'})
|
2011-06-16 23:27:19 +08:00
|
|
|
>>> name.render('name', 'A name')
|
2018-03-12 21:39:09 +08:00
|
|
|
'<input title="Your name" type="text" name="name" value="A name" size="10">'
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2015-09-13 04:27:30 +08:00
|
|
|
If you assign a value of ``True`` or ``False`` to an attribute,
|
|
|
|
it will be rendered as an HTML5 boolean attribute::
|
2014-03-23 01:16:49 +08:00
|
|
|
|
2015-09-13 04:27:30 +08:00
|
|
|
>>> name = forms.TextInput(attrs={'required': True})
|
|
|
|
>>> name.render('name', 'A name')
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input name="name" type="text" value="A name" required>'
|
2015-09-13 04:27:30 +08:00
|
|
|
>>>
|
|
|
|
>>> name = forms.TextInput(attrs={'required': False})
|
|
|
|
>>> name.render('name', 'A name')
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input name="name" type="text" value="A name">'
|
2014-03-23 01:16:49 +08:00
|
|
|
|
2015-03-22 21:27:01 +08:00
|
|
|
.. attribute:: Widget.supports_microseconds
|
|
|
|
|
|
|
|
An attribute that defaults to ``True``. If set to ``False``, the
|
|
|
|
microseconds part of :class:`~datetime.datetime` and
|
|
|
|
:class:`~datetime.time` values will be set to ``0``.
|
|
|
|
|
2016-04-24 01:15:45 +08:00
|
|
|
.. method:: format_value(value)
|
|
|
|
|
|
|
|
Cleans and returns a value for use in the widget template. ``value``
|
|
|
|
isn't guaranteed to be valid input, therefore subclass implementations
|
|
|
|
should program defensively.
|
|
|
|
|
2017-03-20 21:42:59 +08:00
|
|
|
.. method:: get_context(name, value, attrs)
|
2016-12-28 06:00:56 +08:00
|
|
|
|
|
|
|
Returns a dictionary of values to use when rendering the widget
|
|
|
|
template. By default, the dictionary contains a single key,
|
|
|
|
``'widget'``, which is a dictionary representation of the widget
|
|
|
|
containing the following keys:
|
|
|
|
|
|
|
|
* ``'name'``: The name of the field from the ``name`` argument.
|
|
|
|
* ``'is_hidden'``: A boolean indicating whether or not this widget is
|
|
|
|
hidden.
|
|
|
|
* ``'required'``: A boolean indicating whether or not the field for
|
|
|
|
this widget is required.
|
|
|
|
* ``'value'``: The value as returned by :meth:`format_value`.
|
|
|
|
* ``'attrs'``: HTML attributes to be set on the rendered widget. The
|
|
|
|
combination of the :attr:`attrs` attribute and the ``attrs`` argument.
|
|
|
|
* ``'template_name'``: The value of ``self.template_name``.
|
|
|
|
|
|
|
|
``Widget`` subclasses can provide custom context values by overriding
|
|
|
|
this method.
|
|
|
|
|
2017-03-17 08:33:59 +08:00
|
|
|
.. method:: id_for_label(id_)
|
2015-08-10 19:23:24 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
.. method:: render(name, value, attrs=None, renderer=None)
|
2012-09-12 18:59:19 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
Renders a widget to HTML using the given renderer. If ``renderer`` is
|
|
|
|
``None``, the renderer from the :setting:`FORM_RENDERER` setting is
|
|
|
|
used.
|
2012-09-12 18:59:19 +08:00
|
|
|
|
2014-01-23 05:17:32 +08:00
|
|
|
.. method:: value_from_datadict(data, files, name)
|
2012-10-23 19:02:48 +08:00
|
|
|
|
|
|
|
Given a dictionary of data and this widget's name, returns the value
|
2014-08-29 16:22:59 +08:00
|
|
|
of this widget. ``files`` may contain data coming from
|
|
|
|
:attr:`request.FILES <django.http.HttpRequest.FILES>`. Returns ``None``
|
|
|
|
if a value wasn't provided. Note also that ``value_from_datadict`` may
|
|
|
|
be called more than once during handling of form data, so if you
|
|
|
|
customize it and add expensive processing, you should implement some
|
|
|
|
caching mechanism yourself.
|
2012-10-23 19:02:48 +08:00
|
|
|
|
2016-09-07 05:41:54 +08:00
|
|
|
.. method:: value_omitted_from_data(data, files, name)
|
|
|
|
|
|
|
|
Given ``data`` and ``files`` dictionaries and this widget's name,
|
|
|
|
returns whether or not there's data or files for the widget.
|
|
|
|
|
|
|
|
The method's result affects whether or not a field in a model form
|
|
|
|
:ref:`falls back to its default <topics-modelform-save>`.
|
|
|
|
|
2017-03-31 22:10:08 +08:00
|
|
|
Special cases are :class:`~django.forms.CheckboxInput`,
|
|
|
|
:class:`~django.forms.CheckboxSelectMultiple`, and
|
|
|
|
:class:`~django.forms.SelectMultiple`, which always return
|
|
|
|
``False`` because an unchecked checkbox and unselected
|
|
|
|
``<select multiple>`` don't appear in the data of an HTML form
|
|
|
|
submission, so it's unknown whether or not the user submitted a value.
|
2016-09-07 05:41:54 +08:00
|
|
|
|
2016-10-22 23:15:33 +08:00
|
|
|
.. method:: use_required_attribute(initial)
|
|
|
|
|
|
|
|
Given a form field's ``initial`` value, returns whether or not the
|
|
|
|
widget can be rendered with the ``required`` HTML attribute. Forms use
|
|
|
|
this method along with :attr:`Field.required
|
|
|
|
<django.forms.Field.required>` and :attr:`Form.use_required_attribute
|
|
|
|
<django.forms.Form.use_required_attribute>` to determine whether or not
|
|
|
|
to display the ``required`` attribute for each field.
|
|
|
|
|
|
|
|
By default, returns ``False`` for hidden widgets and ``True``
|
2020-01-05 15:51:33 +08:00
|
|
|
otherwise. Special cases are :class:`~django.forms.FileInput` and
|
|
|
|
:class:`~django.forms.ClearableFileInput`, which return ``False`` when
|
|
|
|
``initial`` is set, and :class:`~django.forms.CheckboxSelectMultiple`,
|
|
|
|
which always returns ``False`` because browser validation would require
|
|
|
|
all checkboxes to be checked instead of at least one.
|
2016-10-22 23:15:33 +08:00
|
|
|
|
|
|
|
Override this method in custom widgets that aren't compatible with
|
|
|
|
browser validation. For example, a WSYSIWG text editor widget backed by
|
|
|
|
a hidden ``textarea`` element may want to always return ``False`` to
|
|
|
|
avoid browser validation on the hidden field.
|
|
|
|
|
2016-01-03 18:56:22 +08:00
|
|
|
``MultiWidget``
|
|
|
|
---------------
|
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
.. class:: MultiWidget(widgets, attrs=None)
|
|
|
|
|
|
|
|
A widget that is composed of multiple widgets.
|
2012-12-25 22:56:22 +08:00
|
|
|
:class:`~django.forms.MultiWidget` works hand in hand with the
|
2012-09-12 18:59:19 +08:00
|
|
|
:class:`~django.forms.MultiValueField`.
|
|
|
|
|
2012-10-23 19:02:48 +08:00
|
|
|
:class:`MultiWidget` has one required argument:
|
2012-09-12 18:59:19 +08:00
|
|
|
|
2012-10-23 19:02:48 +08:00
|
|
|
.. attribute:: MultiWidget.widgets
|
2012-09-12 18:59:19 +08:00
|
|
|
|
2020-02-14 06:12:46 +08:00
|
|
|
An iterable containing the widgets needed. For example::
|
|
|
|
|
|
|
|
>>> from django.forms import MultiWidget, TextInput
|
|
|
|
>>> widget = MultiWidget(widgets=[TextInput, TextInput])
|
|
|
|
>>> widget.render('name', ['john', 'paul'])
|
|
|
|
'<input type="text" name="name_0" value="john"><input type="text" name="name_1" value="paul">'
|
|
|
|
|
|
|
|
You may provide a dictionary in order to specify custom suffixes for
|
|
|
|
the ``name`` attribute on each subwidget. In this case, for each
|
|
|
|
``(key, widget)`` pair, the key will be appended to the ``name`` of the
|
|
|
|
widget in order to generate the attribute value. You may provide the
|
2020-03-31 16:37:38 +08:00
|
|
|
empty string (``''``) for a single key, in order to suppress the suffix
|
2020-02-14 06:12:46 +08:00
|
|
|
for one widget. For example::
|
|
|
|
|
|
|
|
>>> widget = MultiWidget(widgets={'': TextInput, 'last': TextInput})
|
2020-12-16 04:12:26 +08:00
|
|
|
>>> widget.render('name', ['john', 'paul'])
|
2020-02-14 06:12:46 +08:00
|
|
|
'<input type="text" name="name" value="john"><input type="text" name="name_last" value="paul">'
|
|
|
|
|
2012-10-23 19:02:48 +08:00
|
|
|
And one required method:
|
2012-09-12 18:59:19 +08:00
|
|
|
|
|
|
|
.. method:: decompress(value)
|
|
|
|
|
2012-10-23 19:02:48 +08:00
|
|
|
This method takes a single "compressed" value from the field and
|
|
|
|
returns a list of "decompressed" values. The input value can be
|
|
|
|
assumed valid, but not necessarily non-empty.
|
2012-09-12 18:59:19 +08:00
|
|
|
|
|
|
|
This method **must be implemented** by the subclass, and since the
|
|
|
|
value may be empty, the implementation must be defensive.
|
|
|
|
|
|
|
|
The rationale behind "decompression" is that it is necessary to "split"
|
2012-10-23 19:02:48 +08:00
|
|
|
the combined value of the form field into the values for each widget.
|
|
|
|
|
|
|
|
An example of this is how :class:`SplitDateTimeWidget` turns a
|
2012-12-25 22:56:22 +08:00
|
|
|
:class:`~datetime.datetime` value into a list with date and time split
|
|
|
|
into two separate values::
|
2012-10-23 19:02:48 +08:00
|
|
|
|
2013-05-19 17:15:35 +08:00
|
|
|
from django.forms import MultiWidget
|
2013-06-07 02:15:26 +08:00
|
|
|
|
2012-10-23 19:02:48 +08:00
|
|
|
class SplitDateTimeWidget(MultiWidget):
|
|
|
|
|
|
|
|
# ...
|
|
|
|
|
|
|
|
def decompress(self, value):
|
|
|
|
if value:
|
2018-01-19 00:23:06 +08:00
|
|
|
return [value.date(), value.time()]
|
2012-10-23 19:02:48 +08:00
|
|
|
return [None, None]
|
2012-09-12 18:59:19 +08:00
|
|
|
|
|
|
|
.. tip::
|
|
|
|
|
|
|
|
Note that :class:`~django.forms.MultiValueField` has a
|
|
|
|
complementary method :meth:`~django.forms.MultiValueField.compress`
|
|
|
|
with the opposite responsibility - to combine cleaned values of
|
|
|
|
all member fields into one.
|
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
It provides some custom context:
|
2012-10-23 19:02:48 +08:00
|
|
|
|
2017-03-20 21:42:59 +08:00
|
|
|
.. method:: get_context(name, value, attrs)
|
2012-10-23 19:02:48 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
In addition to the ``'widget'`` key described in
|
2021-02-01 19:04:48 +08:00
|
|
|
:meth:`Widget.get_context`, ``MultiWidget`` adds a
|
2016-12-28 06:00:56 +08:00
|
|
|
``widget['subwidgets']`` key.
|
2012-10-23 19:02:48 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
These can be looped over in the widget template:
|
2012-10-23 19:02:48 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
.. code-block:: html+django
|
2012-10-23 19:02:48 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
{% for subwidget in widget.subwidgets %}
|
2019-08-31 16:58:41 +08:00
|
|
|
{% include subwidget.template_name with widget=subwidget %}
|
2016-12-28 06:00:56 +08:00
|
|
|
{% endfor %}
|
2012-10-23 19:02:48 +08:00
|
|
|
|
|
|
|
Here's an example widget which subclasses :class:`MultiWidget` to display
|
|
|
|
a date with the day, month, and year in different select boxes. This widget
|
|
|
|
is intended to be used with a :class:`~django.forms.DateField` rather than
|
|
|
|
a :class:`~django.forms.MultiValueField`, thus we have implemented
|
|
|
|
:meth:`~Widget.value_from_datadict`::
|
|
|
|
|
|
|
|
from datetime import date
|
2019-11-23 18:21:53 +08:00
|
|
|
from django import forms
|
2012-10-23 19:02:48 +08:00
|
|
|
|
2019-11-23 18:21:53 +08:00
|
|
|
class DateSelectorWidget(forms.MultiWidget):
|
2012-10-23 19:02:48 +08:00
|
|
|
def __init__(self, attrs=None):
|
2019-11-23 18:21:53 +08:00
|
|
|
days = [(day, day) for day in range(1, 32)]
|
|
|
|
months = [(month, month) for month in range(1, 13)]
|
|
|
|
years = [(year, year) for year in [2018, 2019, 2020]]
|
|
|
|
widgets = [
|
|
|
|
forms.Select(attrs=attrs, choices=days),
|
|
|
|
forms.Select(attrs=attrs, choices=months),
|
|
|
|
forms.Select(attrs=attrs, choices=years),
|
|
|
|
]
|
|
|
|
super().__init__(widgets, attrs)
|
2012-10-23 19:02:48 +08:00
|
|
|
|
|
|
|
def decompress(self, value):
|
2019-11-23 18:21:53 +08:00
|
|
|
if isinstance(value, date):
|
2012-10-23 19:02:48 +08:00
|
|
|
return [value.day, value.month, value.year]
|
2019-11-23 18:21:53 +08:00
|
|
|
elif isinstance(value, str):
|
|
|
|
year, month, day = value.split('-')
|
|
|
|
return [day, month, year]
|
2012-10-23 19:02:48 +08:00
|
|
|
return [None, None, None]
|
|
|
|
|
|
|
|
def value_from_datadict(self, data, files, name):
|
2019-11-23 18:21:53 +08:00
|
|
|
day, month, year = super().value_from_datadict(data, files, name)
|
|
|
|
# DateField expects a single string that it can parse into a date.
|
|
|
|
return '{}-{}-{}'.format(year, month, day)
|
|
|
|
|
|
|
|
The constructor creates several :class:`Select` widgets in a list. The
|
|
|
|
``super()`` method uses this list to setup the widget.
|
2012-10-23 19:02:48 +08:00
|
|
|
|
|
|
|
The required method :meth:`~MultiWidget.decompress` breaks up a
|
|
|
|
``datetime.date`` value into the day, month, and year values corresponding
|
2019-11-23 18:21:53 +08:00
|
|
|
to each widget. If an invalid date was selected, such as the non-existent
|
|
|
|
30th February, the :class:`~django.forms.DateField` passes this method a
|
|
|
|
string instead, so that needs parsing. The final ``return`` handles when
|
|
|
|
``value`` is ``None``, meaning we don't have any defaults for our
|
|
|
|
subwidgets.
|
|
|
|
|
|
|
|
The default implementation of :meth:`~Widget.value_from_datadict` returns a
|
|
|
|
list of values corresponding to each ``Widget``. This is appropriate when
|
|
|
|
using a ``MultiWidget`` with a :class:`~django.forms.MultiValueField`. But
|
|
|
|
since we want to use this widget with a :class:`~django.forms.DateField`,
|
|
|
|
which takes a single value, we have overridden this method. The
|
|
|
|
implementation here combines the data from the subwidgets into a string in
|
|
|
|
the format that :class:`~django.forms.DateField` expects.
|
2012-09-12 18:59:19 +08:00
|
|
|
|
|
|
|
.. _built-in widgets:
|
|
|
|
|
|
|
|
Built-in widgets
|
2016-01-03 18:56:22 +08:00
|
|
|
================
|
2012-09-12 18:59:19 +08:00
|
|
|
|
|
|
|
Django provides a representation of all the basic HTML widgets, plus some
|
|
|
|
commonly used groups of widgets in the ``django.forms.widgets`` module,
|
|
|
|
including :ref:`the input of text <text-widgets>`, :ref:`various checkboxes
|
|
|
|
and selectors <selector-widgets>`, :ref:`uploading files <file-upload-widgets>`,
|
|
|
|
and :ref:`handling of multi-valued input <composite-widgets>`.
|
|
|
|
|
|
|
|
.. _text-widgets:
|
|
|
|
|
|
|
|
Widgets handling input of text
|
2016-01-03 18:56:22 +08:00
|
|
|
------------------------------
|
2012-09-12 18:59:19 +08:00
|
|
|
|
|
|
|
These widgets make use of the HTML elements ``input`` and ``textarea``.
|
|
|
|
|
2011-08-21 19:51:48 +08:00
|
|
|
``TextInput``
|
|
|
|
~~~~~~~~~~~~~
|
2011-06-16 23:27:19 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: TextInput
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``input_type``: ``'text'``
|
|
|
|
* ``template_name``: ``'django/forms/widgets/text.html'``
|
|
|
|
* Renders as: ``<input type="text" ...>``
|
2013-01-28 21:12:56 +08:00
|
|
|
|
2013-02-23 16:45:56 +08:00
|
|
|
``NumberInput``
|
|
|
|
~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. class:: NumberInput
|
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``input_type``: ``'number'``
|
|
|
|
* ``template_name``: ``'django/forms/widgets/number.html'``
|
|
|
|
* Renders as: ``<input type="number" ...>``
|
2013-02-23 16:45:56 +08:00
|
|
|
|
|
|
|
Beware that not all browsers support entering localized numbers in
|
|
|
|
``number`` input types. Django itself avoids using them for fields having
|
2015-05-26 22:27:55 +08:00
|
|
|
their :attr:`~django.forms.Field.localize` property set to ``True``.
|
2013-02-23 16:45:56 +08:00
|
|
|
|
2013-01-28 21:12:56 +08:00
|
|
|
``EmailInput``
|
|
|
|
~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. class:: EmailInput
|
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``input_type``: ``'email'``
|
|
|
|
* ``template_name``: ``'django/forms/widgets/email.html'``
|
|
|
|
* Renders as: ``<input type="email" ...>``
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-01-28 21:24:48 +08:00
|
|
|
``URLInput``
|
|
|
|
~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. class:: URLInput
|
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``input_type``: ``'url'``
|
|
|
|
* ``template_name``: ``'django/forms/widgets/url.html'``
|
|
|
|
* Renders as: ``<input type="url" ...>``
|
2013-01-28 21:24:48 +08:00
|
|
|
|
2011-08-21 19:51:48 +08:00
|
|
|
``PasswordInput``
|
|
|
|
~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: PasswordInput
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``input_type``: ``'password'``
|
|
|
|
* ``template_name``: ``'django/forms/widgets/password.html'``
|
2017-04-14 14:44:11 +08:00
|
|
|
* Renders as: ``<input type="password" ...>``
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2009-08-29 20:40:47 +08:00
|
|
|
Takes one optional argument:
|
|
|
|
|
|
|
|
.. attribute:: PasswordInput.render_value
|
|
|
|
|
|
|
|
Determines whether the widget will have a value filled in when the
|
2010-08-06 22:25:58 +08:00
|
|
|
form is re-displayed after a validation error (default is ``False``).
|
|
|
|
|
2011-08-21 19:51:48 +08:00
|
|
|
``HiddenInput``
|
|
|
|
~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: HiddenInput
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``input_type``: ``'hidden'``
|
|
|
|
* ``template_name``: ``'django/forms/widgets/hidden.html'``
|
2017-04-14 14:44:11 +08:00
|
|
|
* Renders as: ``<input type="hidden" ...>``
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
Note that there also is a :class:`MultipleHiddenInput` widget that
|
|
|
|
encapsulates a set of hidden input elements.
|
2010-10-01 10:02:58 +08:00
|
|
|
|
2011-08-21 19:51:48 +08:00
|
|
|
``DateInput``
|
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
2009-03-23 00:13:06 +08:00
|
|
|
.. class:: DateInput
|
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``input_type``: ``'text'``
|
|
|
|
* ``template_name``: ``'django/forms/widgets/date.html'``
|
2017-04-14 14:44:11 +08:00
|
|
|
* Renders as: ``<input type="text" ...>``
|
2009-03-23 00:13:06 +08:00
|
|
|
|
2012-09-11 01:21:29 +08:00
|
|
|
Takes same arguments as :class:`TextInput`, with one more optional argument:
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2009-03-23 00:13:06 +08:00
|
|
|
.. attribute:: DateInput.format
|
|
|
|
|
|
|
|
The format in which this field's initial value will be displayed.
|
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
If no ``format`` argument is provided, the default format is the first
|
|
|
|
format found in :setting:`DATE_INPUT_FORMATS` and respects
|
2016-01-03 18:56:22 +08:00
|
|
|
:doc:`/topics/i18n/formatting`.
|
2009-03-23 00:13:06 +08:00
|
|
|
|
2011-08-21 19:51:48 +08:00
|
|
|
``DateTimeInput``
|
|
|
|
~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: DateTimeInput
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``input_type``: ``'text'``
|
|
|
|
* ``template_name``: ``'django/forms/widgets/datetime.html'``
|
2017-04-14 14:44:11 +08:00
|
|
|
* Renders as: ``<input type="text" ...>``
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2012-09-11 01:21:29 +08:00
|
|
|
Takes same arguments as :class:`TextInput`, with one more optional argument:
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2009-03-23 00:13:06 +08:00
|
|
|
.. attribute:: DateTimeInput.format
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2009-03-23 00:13:06 +08:00
|
|
|
The format in which this field's initial value will be displayed.
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
If no ``format`` argument is provided, the default format is the first
|
|
|
|
format found in :setting:`DATETIME_INPUT_FORMATS` and respects
|
2016-01-03 18:56:22 +08:00
|
|
|
:doc:`/topics/i18n/formatting`.
|
2009-03-23 00:13:06 +08:00
|
|
|
|
2015-03-22 21:27:01 +08:00
|
|
|
By default, the microseconds part of the time value is always set to ``0``.
|
|
|
|
If microseconds are required, use a subclass with the
|
|
|
|
:attr:`~Widget.supports_microseconds` attribute set to ``True``.
|
|
|
|
|
2011-08-21 19:51:48 +08:00
|
|
|
``TimeInput``
|
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
2009-03-23 00:13:06 +08:00
|
|
|
.. class:: TimeInput
|
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``input_type``: ``'text'``
|
|
|
|
* ``template_name``: ``'django/forms/widgets/time.html'``
|
2017-04-14 14:44:11 +08:00
|
|
|
* Renders as: ``<input type="text" ...>``
|
2009-03-23 00:13:06 +08:00
|
|
|
|
2012-09-11 01:21:29 +08:00
|
|
|
Takes same arguments as :class:`TextInput`, with one more optional argument:
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2009-03-23 00:13:06 +08:00
|
|
|
.. attribute:: TimeInput.format
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2009-03-23 00:13:06 +08:00
|
|
|
The format in which this field's initial value will be displayed.
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
If no ``format`` argument is provided, the default format is the first
|
|
|
|
format found in :setting:`TIME_INPUT_FORMATS` and respects
|
2016-01-03 18:56:22 +08:00
|
|
|
:doc:`/topics/i18n/formatting`.
|
2009-03-23 00:13:06 +08:00
|
|
|
|
2015-03-22 21:27:01 +08:00
|
|
|
For the treatment of microseconds, see :class:`DateTimeInput`.
|
|
|
|
|
2011-08-21 19:51:48 +08:00
|
|
|
``Textarea``
|
|
|
|
~~~~~~~~~~~~
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: Textarea
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``template_name``: ``'django/forms/widgets/textarea.html'``
|
|
|
|
* Renders as: ``<textarea>...</textarea>``
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
.. _selector-widgets:
|
|
|
|
|
|
|
|
Selector and checkbox widgets
|
2016-01-03 18:56:22 +08:00
|
|
|
-----------------------------
|
2012-09-12 18:59:19 +08:00
|
|
|
|
2017-03-11 15:11:44 +08:00
|
|
|
These widgets make use of the HTML elements ``<select>``,
|
2017-04-14 14:44:11 +08:00
|
|
|
``<input type="checkbox">``, and ``<input type="radio">``.
|
2017-03-11 15:11:44 +08:00
|
|
|
|
|
|
|
Widgets that render multiple choices have an ``option_template_name`` attribute
|
|
|
|
that specifies the template used to render each choice. For example, for the
|
|
|
|
:class:`Select` widget, ``select_option.html`` renders the ``<option>`` for a
|
|
|
|
``<select>``.
|
|
|
|
|
2011-08-21 19:51:48 +08:00
|
|
|
``CheckboxInput``
|
|
|
|
~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: CheckboxInput
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``input_type``: ``'checkbox'``
|
|
|
|
* ``template_name``: ``'django/forms/widgets/checkbox.html'``
|
2017-04-14 14:44:11 +08:00
|
|
|
* Renders as: ``<input type="checkbox" ...>``
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2009-08-29 20:40:47 +08:00
|
|
|
Takes one optional argument:
|
|
|
|
|
|
|
|
.. attribute:: CheckboxInput.check_test
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2014-04-05 22:26:11 +08:00
|
|
|
A callable that takes the value of the ``CheckboxInput`` and returns
|
2011-06-16 23:27:19 +08:00
|
|
|
``True`` if the checkbox should be checked for that value.
|
2009-08-29 20:40:47 +08:00
|
|
|
|
2011-08-21 19:51:48 +08:00
|
|
|
``Select``
|
|
|
|
~~~~~~~~~~
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: Select
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``template_name``: ``'django/forms/widgets/select.html'``
|
2017-03-11 15:11:44 +08:00
|
|
|
* ``option_template_name``: ``'django/forms/widgets/select_option.html'``
|
2016-12-28 06:00:56 +08:00
|
|
|
* Renders as: ``<select><option ...>...</select>``
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
.. attribute:: Select.choices
|
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
This attribute is optional when the form field does not have a
|
|
|
|
``choices`` attribute. If it does, it will override anything you set
|
|
|
|
here when the attribute is updated on the :class:`Field`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-08-21 19:51:48 +08:00
|
|
|
``NullBooleanSelect``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: NullBooleanSelect
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``template_name``: ``'django/forms/widgets/select.html'``
|
2017-03-11 15:11:44 +08:00
|
|
|
* ``option_template_name``: ``'django/forms/widgets/select_option.html'``
|
2016-12-28 06:00:56 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Select widget with options 'Unknown', 'Yes' and 'No'
|
|
|
|
|
2011-08-21 19:51:48 +08:00
|
|
|
``SelectMultiple``
|
|
|
|
~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: SelectMultiple
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``template_name``: ``'django/forms/widgets/select.html'``
|
2017-03-11 15:11:44 +08:00
|
|
|
* ``option_template_name``: ``'django/forms/widgets/select_option.html'``
|
2016-12-28 06:00:56 +08:00
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
Similar to :class:`Select`, but allows multiple selection:
|
2018-01-21 00:19:06 +08:00
|
|
|
``<select multiple>...</select>``
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
|
2011-08-21 19:51:48 +08:00
|
|
|
``RadioSelect``
|
|
|
|
~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: RadioSelect
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``template_name``: ``'django/forms/widgets/radio.html'``
|
2017-03-11 15:11:44 +08:00
|
|
|
* ``option_template_name``: ``'django/forms/widgets/radio_option.html'``
|
2016-12-28 06:00:56 +08:00
|
|
|
|
2011-12-08 06:31:39 +08:00
|
|
|
Similar to :class:`Select`, but rendered as a list of radio buttons within
|
|
|
|
``<li>`` tags:
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. code-block:: html
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
<ul>
|
2017-04-14 14:44:11 +08:00
|
|
|
<li><input type="radio" name="..."></li>
|
2008-08-24 06:25:40 +08:00
|
|
|
...
|
|
|
|
</ul>
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2011-12-08 06:31:39 +08:00
|
|
|
For more granular control over the generated markup, you can loop over the
|
|
|
|
radio buttons in the template. Assuming a form ``myform`` with a field
|
|
|
|
``beatles`` that uses a ``RadioSelect`` as its widget:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
{% for radio in myform.beatles %}
|
|
|
|
<div class="myradio">
|
|
|
|
{{ radio }}
|
|
|
|
</div>
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
This would generate the following HTML:
|
|
|
|
|
|
|
|
.. code-block:: html
|
|
|
|
|
|
|
|
<div class="myradio">
|
2018-01-21 15:09:10 +08:00
|
|
|
<label for="id_beatles_0"><input id="id_beatles_0" name="beatles" type="radio" value="john" required> John</label>
|
2011-12-08 06:31:39 +08:00
|
|
|
</div>
|
|
|
|
<div class="myradio">
|
2018-01-21 15:09:10 +08:00
|
|
|
<label for="id_beatles_1"><input id="id_beatles_1" name="beatles" type="radio" value="paul" required> Paul</label>
|
2011-12-08 06:31:39 +08:00
|
|
|
</div>
|
|
|
|
<div class="myradio">
|
2018-01-21 15:09:10 +08:00
|
|
|
<label for="id_beatles_2"><input id="id_beatles_2" name="beatles" type="radio" value="george" required> George</label>
|
2011-12-08 06:31:39 +08:00
|
|
|
</div>
|
|
|
|
<div class="myradio">
|
2018-01-21 15:09:10 +08:00
|
|
|
<label for="id_beatles_3"><input id="id_beatles_3" name="beatles" type="radio" value="ringo" required> Ringo</label>
|
2011-12-08 06:31:39 +08:00
|
|
|
</div>
|
|
|
|
|
2011-12-08 07:08:27 +08:00
|
|
|
That included the ``<label>`` tags. To get more granular, you can use each
|
2013-06-05 04:41:49 +08:00
|
|
|
radio button's ``tag``, ``choice_label`` and ``id_for_label`` attributes.
|
|
|
|
For example, this template...
|
2011-12-08 07:08:27 +08:00
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
{% for radio in myform.beatles %}
|
2013-06-05 04:41:49 +08:00
|
|
|
<label for="{{ radio.id_for_label }}">
|
2011-12-08 07:08:27 +08:00
|
|
|
{{ radio.choice_label }}
|
|
|
|
<span class="radio">{{ radio.tag }}</span>
|
|
|
|
</label>
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
...will result in the following HTML:
|
|
|
|
|
|
|
|
.. code-block:: html
|
|
|
|
|
2013-06-05 04:41:49 +08:00
|
|
|
<label for="id_beatles_0">
|
|
|
|
John
|
2018-01-21 15:09:10 +08:00
|
|
|
<span class="radio"><input id="id_beatles_0" name="beatles" type="radio" value="john" required></span>
|
2013-06-05 04:41:49 +08:00
|
|
|
</label>
|
|
|
|
|
|
|
|
<label for="id_beatles_1">
|
|
|
|
Paul
|
2018-01-21 15:09:10 +08:00
|
|
|
<span class="radio"><input id="id_beatles_1" name="beatles" type="radio" value="paul" required></span>
|
2013-06-05 04:41:49 +08:00
|
|
|
</label>
|
|
|
|
|
|
|
|
<label for="id_beatles_2">
|
|
|
|
George
|
2018-01-21 15:09:10 +08:00
|
|
|
<span class="radio"><input id="id_beatles_2" name="beatles" type="radio" value="george" required></span>
|
2013-06-05 04:41:49 +08:00
|
|
|
</label>
|
2011-12-08 07:08:27 +08:00
|
|
|
|
2013-06-05 04:41:49 +08:00
|
|
|
<label for="id_beatles_3">
|
|
|
|
Ringo
|
2018-01-21 15:09:10 +08:00
|
|
|
<span class="radio"><input id="id_beatles_3" name="beatles" type="radio" value="ringo" required></span>
|
2013-06-05 04:41:49 +08:00
|
|
|
</label>
|
|
|
|
|
|
|
|
If you decide not to loop over the radio buttons -- e.g., if your template
|
2019-06-17 22:54:55 +08:00
|
|
|
includes ``{{ myform.beatles }}`` -- they'll be output in a ``<ul>`` with
|
|
|
|
``<li>`` tags, as above.
|
2011-12-08 06:31:39 +08:00
|
|
|
|
2016-03-18 00:45:23 +08:00
|
|
|
The outer ``<ul>`` container receives the ``id`` attribute of the widget,
|
|
|
|
if defined, or :attr:`BoundField.auto_id` otherwise.
|
2013-04-13 05:22:31 +08:00
|
|
|
|
2013-06-05 04:41:49 +08:00
|
|
|
When looping over the radio buttons, the ``label`` and ``input`` tags include
|
|
|
|
``for`` and ``id`` attributes, respectively. Each radio button has an
|
|
|
|
``id_for_label`` attribute to output the element's ID.
|
|
|
|
|
2011-08-21 19:51:48 +08:00
|
|
|
``CheckboxSelectMultiple``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: CheckboxSelectMultiple
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``template_name``: ``'django/forms/widgets/checkbox_select.html'``
|
2017-03-11 15:11:44 +08:00
|
|
|
* ``option_template_name``: ``'django/forms/widgets/checkbox_option.html'``
|
2016-12-28 06:00:56 +08:00
|
|
|
|
2018-03-12 21:39:09 +08:00
|
|
|
Similar to :class:`SelectMultiple`, but rendered as a list of checkboxes:
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. code-block:: html
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
<ul>
|
2017-04-14 14:44:11 +08:00
|
|
|
<li><input type="checkbox" name="..." ></li>
|
2008-08-24 06:25:40 +08:00
|
|
|
...
|
|
|
|
</ul>
|
|
|
|
|
2016-03-18 00:45:23 +08:00
|
|
|
The outer ``<ul>`` container receives the ``id`` attribute of the widget,
|
|
|
|
if defined, or :attr:`BoundField.auto_id` otherwise.
|
2013-04-07 16:37:38 +08:00
|
|
|
|
2016-07-22 08:16:22 +08:00
|
|
|
Like :class:`RadioSelect`, you can loop over the individual checkboxes for the
|
|
|
|
widget's choices. Unlike :class:`RadioSelect`, the checkboxes won't include the
|
|
|
|
``required`` HTML attribute if the field is required because browser validation
|
|
|
|
would require all checkboxes to be checked instead of at least one.
|
2013-04-13 08:02:28 +08:00
|
|
|
|
2015-01-27 04:39:52 +08:00
|
|
|
When looping over the checkboxes, the ``label`` and ``input`` tags include
|
|
|
|
``for`` and ``id`` attributes, respectively. Each checkbox has an
|
|
|
|
``id_for_label`` attribute to output the element's ID.
|
2013-06-05 04:41:49 +08:00
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
.. _file-upload-widgets:
|
|
|
|
|
|
|
|
File upload widgets
|
2016-01-03 18:56:22 +08:00
|
|
|
-------------------
|
2012-09-12 18:59:19 +08:00
|
|
|
|
|
|
|
``FileInput``
|
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. class:: FileInput
|
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``template_name``: ``'django/forms/widgets/file.html'``
|
2017-04-14 14:44:11 +08:00
|
|
|
* Renders as: ``<input type="file" ...>``
|
2012-09-12 18:59:19 +08:00
|
|
|
|
|
|
|
``ClearableFileInput``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. class:: ClearableFileInput
|
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``template_name``: ``'django/forms/widgets/clearable_file_input.html'``
|
2017-04-14 14:44:11 +08:00
|
|
|
* Renders as: ``<input type="file" ...>`` with an additional checkbox
|
2016-12-28 06:00:56 +08:00
|
|
|
input to clear the field's value, if the field is not required and has
|
|
|
|
initial data.
|
2012-09-12 18:59:19 +08:00
|
|
|
|
|
|
|
.. _composite-widgets:
|
|
|
|
|
|
|
|
Composite widgets
|
2016-01-03 18:56:22 +08:00
|
|
|
-----------------
|
2012-09-12 18:59:19 +08:00
|
|
|
|
|
|
|
``MultipleHiddenInput``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. class:: MultipleHiddenInput
|
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``template_name``: ``'django/forms/widgets/multiple_hidden.html'``
|
2017-04-14 14:44:11 +08:00
|
|
|
* Renders as: multiple ``<input type="hidden" ...>`` tags
|
2012-09-12 18:59:19 +08:00
|
|
|
|
|
|
|
A widget that handles multiple hidden widgets for fields that have a list
|
|
|
|
of values.
|
|
|
|
|
2011-08-21 19:51:48 +08:00
|
|
|
``SplitDateTimeWidget``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
.. class:: SplitDateTimeWidget
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``template_name``: ``'django/forms/widgets/splitdatetime.html'``
|
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
Wrapper (using :class:`MultiWidget`) around two widgets: :class:`DateInput`
|
2016-05-08 08:01:15 +08:00
|
|
|
for the date, and :class:`TimeInput` for the time. Must be used with
|
|
|
|
:class:`SplitDateTimeField` rather than :class:`DateTimeField`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2017-01-13 19:48:30 +08:00
|
|
|
``SplitDateTimeWidget`` has several optional arguments:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
.. attribute:: SplitDateTimeWidget.date_format
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
Similar to :attr:`DateInput.format`
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
.. attribute:: SplitDateTimeWidget.time_format
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
Similar to :attr:`TimeInput.format`
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2017-01-13 19:48:30 +08:00
|
|
|
.. attribute:: SplitDateTimeWidget.date_attrs
|
|
|
|
.. attribute:: SplitDateTimeWidget.time_attrs
|
|
|
|
|
|
|
|
Similar to :attr:`Widget.attrs`. A dictionary containing HTML
|
|
|
|
attributes to be set on the rendered :class:`DateInput` and
|
|
|
|
:class:`TimeInput` widgets, respectively. If these attributes aren't
|
|
|
|
set, :attr:`Widget.attrs` is used instead.
|
|
|
|
|
2011-08-21 19:51:48 +08:00
|
|
|
``SplitHiddenDateTimeWidget``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
.. class:: SplitHiddenDateTimeWidget
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``template_name``: ``'django/forms/widgets/splithiddendatetime.html'``
|
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
Similar to :class:`SplitDateTimeWidget`, but uses :class:`HiddenInput` for
|
|
|
|
both date and time.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-08-21 19:51:48 +08:00
|
|
|
``SelectDateWidget``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
.. class:: SelectDateWidget
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
* ``template_name``: ``'django/forms/widgets/select_date.html'``
|
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
Wrapper around three :class:`~django.forms.Select` widgets: one each for
|
2015-07-15 21:57:55 +08:00
|
|
|
month, day, and year.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2015-07-15 21:55:44 +08:00
|
|
|
Takes several optional arguments:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
.. attribute:: SelectDateWidget.years
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-06-16 23:27:19 +08:00
|
|
|
An optional list/tuple of years to use in the "year" select box.
|
|
|
|
The default is a list containing the current year and the next 9 years.
|
2013-08-28 14:22:47 +08:00
|
|
|
|
|
|
|
.. attribute:: SelectDateWidget.months
|
|
|
|
|
|
|
|
An optional dict of months to use in the "months" select box.
|
|
|
|
|
|
|
|
The keys of the dict correspond to the month number (1-indexed) and
|
2014-08-18 22:30:44 +08:00
|
|
|
the values are the displayed months::
|
2013-08-28 14:22:47 +08:00
|
|
|
|
|
|
|
MONTHS = {
|
|
|
|
1:_('jan'), 2:_('feb'), 3:_('mar'), 4:_('apr'),
|
|
|
|
5:_('may'), 6:_('jun'), 7:_('jul'), 8:_('aug'),
|
|
|
|
9:_('sep'), 10:_('oct'), 11:_('nov'), 12:_('dec')
|
|
|
|
}
|
2014-05-23 15:07:15 +08:00
|
|
|
|
|
|
|
.. attribute:: SelectDateWidget.empty_label
|
|
|
|
|
|
|
|
If the :class:`~django.forms.DateField` is not required,
|
2014-05-29 23:15:01 +08:00
|
|
|
:class:`SelectDateWidget` will have an empty choice at the top of the
|
|
|
|
list (which is ``---`` by default). You can change the text of this
|
|
|
|
label with the ``empty_label`` attribute. ``empty_label`` can be a
|
|
|
|
``string``, ``list``, or ``tuple``. When a string is used, all select
|
|
|
|
boxes will each have an empty choice with this label. If ``empty_label``
|
|
|
|
is a ``list`` or ``tuple`` of 3 string elements, the select boxes will
|
|
|
|
have their own custom label. The labels should be in this order
|
|
|
|
``('year_label', 'month_label', 'day_label')``.
|
2014-05-23 15:07:15 +08:00
|
|
|
|
2014-05-29 23:15:01 +08:00
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# A custom empty label with string
|
2014-05-23 15:07:15 +08:00
|
|
|
field1 = forms.DateField(widget=SelectDateWidget(empty_label="Nothing"))
|
2014-05-29 23:15:01 +08:00
|
|
|
|
|
|
|
# A custom empty label with tuple
|
2015-06-09 03:19:16 +08:00
|
|
|
field1 = forms.DateField(
|
|
|
|
widget=SelectDateWidget(
|
|
|
|
empty_label=("Choose Year", "Choose Month", "Choose Day"),
|
|
|
|
),
|
|
|
|
)
|