2008-08-23 22:25:40 +00:00
|
|
|
=======
|
|
|
|
Widgets
|
|
|
|
=======
|
|
|
|
|
|
|
|
.. module:: django.forms.widgets
|
|
|
|
:synopsis: Django's built-in form widgets.
|
2010-08-06 14:25:58 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. currentmodule:: django.forms
|
|
|
|
|
|
|
|
A widget is Django's representation of a HTML input element. The widget
|
|
|
|
handles the rendering of the HTML, and the extraction of data from a GET/POST
|
|
|
|
dictionary that corresponds to the widget.
|
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
Specifying widgets
|
|
|
|
------------------
|
|
|
|
|
|
|
|
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
|
|
|
|
:ref:`built-in fields`.
|
|
|
|
|
|
|
|
However, if you want to use a different widget for a field, you can
|
|
|
|
just use the :attr:`~Field.widget` argument on the field definition. For
|
2011-10-14 00:12:01 +00:00
|
|
|
example::
|
2011-06-16 15:27:19 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
from django import forms
|
2011-06-16 15:27:19 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
class CommentForm(forms.Form):
|
|
|
|
name = forms.CharField()
|
|
|
|
url = forms.URLField()
|
|
|
|
comment = forms.CharField(widget=forms.Textarea)
|
2011-06-16 15:27:19 +00: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
|
|
|
|
-----------------------------
|
|
|
|
|
|
|
|
Many widgets have optional extra arguments; they can be set when defining the
|
|
|
|
widget on the field. In the following example, the
|
|
|
|
:attr:`~SelectDateWidget.years` attribute is set for a
|
2011-10-14 00:12:01 +00:00
|
|
|
:class:`~django.forms.extras.widgets.SelectDateWidget`::
|
2011-06-16 15:27:19 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
from django.forms.fields import DateField, ChoiceField, MultipleChoiceField
|
|
|
|
from django.forms.widgets import RadioSelect, CheckboxSelectMultiple
|
|
|
|
from django.forms.extras.widgets import SelectDateWidget
|
2011-06-16 15:27:19 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
BIRTH_YEAR_CHOICES = ('1980', '1981', '1982')
|
|
|
|
GENDER_CHOICES = (('m', 'Male'), ('f', 'Female'))
|
|
|
|
FAVORITE_COLORS_CHOICES = (('blue', 'Blue'),
|
|
|
|
('green', 'Green'),
|
|
|
|
('black', 'Black'))
|
2011-06-16 15:27:19 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
class SimpleForm(forms.Form):
|
|
|
|
birth_year = DateField(widget=SelectDateWidget(years=BIRTH_YEAR_CHOICES))
|
|
|
|
gender = ChoiceField(widget=RadioSelect, choices=GENDER_CHOICES)
|
|
|
|
favorite_colors = forms.MultipleChoiceField(required=False,
|
|
|
|
widget=CheckboxSelectMultiple, choices=FAVORITE_COLORS_CHOICES)
|
2011-06-16 15:27:19 +00:00
|
|
|
|
|
|
|
See the :ref:`built-in widgets` for more information about which widgets
|
|
|
|
are available and which arguments they accept.
|
|
|
|
|
|
|
|
|
|
|
|
Widgets inheriting from the Select widget
|
|
|
|
-----------------------------------------
|
|
|
|
|
|
|
|
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 00:12:01 +00:00
|
|
|
example::
|
2011-06-16 15:27:19 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
>>> from django import forms
|
|
|
|
>>> CHOICES = (('1', 'First',), ('2', 'Second',)))
|
|
|
|
>>> choice_field = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES)
|
|
|
|
>>> choice_field.choices
|
|
|
|
[('1', 'First'), ('2', 'Second')]
|
|
|
|
>>> choice_field.widget.choices
|
|
|
|
[('1', 'First'), ('2', 'Second')]
|
|
|
|
>>> choice_field.widget.choices = ()
|
|
|
|
>>> choice_field.choices = (('1', 'First and only',),)
|
|
|
|
>>> choice_field.widget.choices
|
|
|
|
[('1', 'First and only')]
|
2011-06-16 15:27:19 +00: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
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
When Django renders a widget as HTML, it only renders the bare minimum
|
|
|
|
HTML - Django doesn't add a class definition, or any other widget-specific
|
|
|
|
attributes. This means that all :class:`TextInput` widgets will appear the same
|
|
|
|
on your Web page.
|
|
|
|
|
|
|
|
If you want to make one widget look different to another, you need to
|
|
|
|
specify additional attributes for each widget. When you specify a
|
|
|
|
widget, you can provide a list of attributes that will be added to the
|
|
|
|
rendered HTML for the widget.
|
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
For example, take the following simple form::
|
2011-06-16 15:27:19 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
from django import forms
|
2011-06-16 15:27:19 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
class CommentForm(forms.Form):
|
|
|
|
name = forms.CharField()
|
|
|
|
url = forms.URLField()
|
|
|
|
comment = forms.CharField()
|
2011-06-16 15:27:19 +00: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 00:12:01 +00:00
|
|
|
provided for each widget will be rendered exactly the same::
|
2011-06-16 15:27:19 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
>>> f = CommentForm(auto_id=False)
|
|
|
|
>>> f.as_table()
|
|
|
|
<tr><th>Name:</th><td><input type="text" name="name" /></td></tr>
|
|
|
|
<tr><th>Url:</th><td><input type="text" name="url"/></td></tr>
|
|
|
|
<tr><th>Comment:</th><td><input type="text" name="comment" /></td></tr>
|
2011-06-16 15:27:19 +00: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
|
|
|
|
'name' widget to have some special CSS class. To do this, you use the
|
|
|
|
:attr:`Widget.attrs` argument when creating the widget:
|
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
For example::
|
2011-06-16 15:27:19 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
class CommentForm(forms.Form):
|
|
|
|
name = forms.CharField(
|
|
|
|
widget=forms.TextInput(attrs={'class':'special'}))
|
|
|
|
url = forms.URLField()
|
|
|
|
comment = forms.CharField(
|
|
|
|
widget=forms.TextInput(attrs={'size':'40'}))
|
2011-06-16 15:27:19 +00:00
|
|
|
|
|
|
|
Django will then include the extra attributes in the rendered output:
|
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
>>> f = CommentForm(auto_id=False)
|
|
|
|
>>> f.as_table()
|
|
|
|
<tr><th>Name:</th><td><input type="text" name="name" class="special"/></td></tr>
|
|
|
|
<tr><th>Url:</th><td><input type="text" name="url"/></td></tr>
|
|
|
|
<tr><th>Comment:</th><td><input type="text" name="comment" size="40"/></td></tr>
|
2011-06-16 15:27:19 +00:00
|
|
|
|
|
|
|
.. _built-in widgets:
|
|
|
|
|
|
|
|
Built-in widgets
|
|
|
|
----------------
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Django provides a representation of all the basic HTML widgets, plus some
|
|
|
|
commonly used groups of widgets:
|
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``Widget``
|
|
|
|
~~~~~~~~~~
|
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
.. class:: Widget
|
|
|
|
|
|
|
|
This abstract class cannot be rendered, but provides the basic attribute :attr:`~Widget.attrs`.
|
|
|
|
|
|
|
|
.. attribute:: Widget.attrs
|
|
|
|
|
|
|
|
A dictionary containing HTML attributes to be set on the rendered widget.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
>>> name = forms.TextInput(attrs={'size': 10, 'title': 'Your name',})
|
|
|
|
>>> name.render('name', 'A name')
|
|
|
|
u'<input title="Your name" type="text" name="name" value="A name" size="10" />'
|
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``TextInput``
|
|
|
|
~~~~~~~~~~~~~
|
2011-06-16 15:27:19 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. class:: TextInput
|
2008-08-27 05:56:57 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Text input: ``<input type='text' ...>``
|
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``PasswordInput``
|
|
|
|
~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. class:: PasswordInput
|
2008-08-27 05:56:57 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Password input: ``<input type='password' ...>``
|
|
|
|
|
2009-08-29 12:40:47 +00:00
|
|
|
Takes one optional argument:
|
|
|
|
|
|
|
|
.. attribute:: PasswordInput.render_value
|
|
|
|
|
|
|
|
Determines whether the widget will have a value filled in when the
|
2010-08-06 14:25:58 +00:00
|
|
|
form is re-displayed after a validation error (default is ``False``).
|
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
.. versionchanged:: 1.3
|
|
|
|
The default value for
|
|
|
|
:attr:`~PasswordInput.render_value` was
|
|
|
|
changed from ``True`` to ``False``
|
2009-08-29 12:40:47 +00:00
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``HiddenInput``
|
|
|
|
~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. class:: HiddenInput
|
2008-08-27 05:56:57 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Hidden input: ``<input type='hidden' ...>``
|
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``MultipleHiddenInput``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. class:: MultipleHiddenInput
|
2008-08-27 05:56:57 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Multiple ``<input type='hidden' ...>`` widgets.
|
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
A widget that handles multiple hidden widgets for fields that have a list
|
|
|
|
of values.
|
|
|
|
|
|
|
|
.. attribute:: MultipleHiddenInput.choices
|
|
|
|
|
|
|
|
This attribute is optional when the field does not have a
|
|
|
|
:attr:`~Field.choices` attribute. If it does, it will override anything
|
|
|
|
you set here when the attribute is updated on the :class:`Field`.
|
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``FileInput``
|
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. class:: FileInput
|
2008-08-27 05:56:57 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
File upload input: ``<input type='file' ...>``
|
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``ClearableFileInput``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2010-10-01 02:02:58 +00:00
|
|
|
.. class:: ClearableFileInput
|
|
|
|
|
|
|
|
.. versionadded:: 1.3
|
|
|
|
|
|
|
|
File upload input: ``<input type='file' ...>``, with an additional checkbox
|
|
|
|
input to clear the field's value, if the field is not required and has
|
|
|
|
initial data.
|
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``DateInput``
|
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
2009-03-22 16:13:06 +00:00
|
|
|
.. class:: DateInput
|
|
|
|
|
|
|
|
Date input as a simple text box: ``<input type='text' ...>``
|
|
|
|
|
|
|
|
Takes one optional argument:
|
2010-08-06 14:25:58 +00:00
|
|
|
|
2009-03-22 16:13:06 +00:00
|
|
|
.. attribute:: DateInput.format
|
|
|
|
|
|
|
|
The format in which this field's initial value will be displayed.
|
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
If no ``format`` argument is provided, the default format is the first
|
|
|
|
format found in :setting:`DATE_INPUT_FORMATS` and respects
|
|
|
|
:ref:`format-localization`.
|
2009-03-22 16:13:06 +00:00
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``DateTimeInput``
|
|
|
|
~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. class:: DateTimeInput
|
2008-08-27 05:56:57 +00:00
|
|
|
|
2008-09-02 03:40:42 +00:00
|
|
|
Date/time input as a simple text box: ``<input type='text' ...>``
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2009-03-22 16:13:06 +00:00
|
|
|
Takes one optional argument:
|
2010-08-06 14:25:58 +00:00
|
|
|
|
2009-03-22 16:13:06 +00:00
|
|
|
.. attribute:: DateTimeInput.format
|
2010-08-06 14:25:58 +00:00
|
|
|
|
2009-03-22 16:13:06 +00:00
|
|
|
The format in which this field's initial value will be displayed.
|
2010-08-06 14:25:58 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
If no ``format`` argument is provided, the default format is the first
|
|
|
|
format found in :setting:`DATETIME_INPUT_FORMATS` and respects
|
|
|
|
:ref:`format-localization`.
|
2009-03-22 16:13:06 +00:00
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``TimeInput``
|
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
2009-03-22 16:13:06 +00:00
|
|
|
.. class:: TimeInput
|
|
|
|
|
|
|
|
Time input as a simple text box: ``<input type='text' ...>``
|
|
|
|
|
|
|
|
Takes one optional argument:
|
2010-08-06 14:25:58 +00:00
|
|
|
|
2009-03-22 16:13:06 +00:00
|
|
|
.. attribute:: TimeInput.format
|
2010-08-06 14:25:58 +00:00
|
|
|
|
2009-03-22 16:13:06 +00:00
|
|
|
The format in which this field's initial value will be displayed.
|
2010-08-06 14:25:58 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
If no ``format`` argument is provided, the default format is the first
|
|
|
|
format found in :setting:`TIME_INPUT_FORMATS` and respects
|
|
|
|
:ref:`format-localization`.
|
2009-03-22 16:13:06 +00:00
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``Textarea``
|
|
|
|
~~~~~~~~~~~~
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. class:: Textarea
|
2008-08-27 05:56:57 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Text area: ``<textarea>...</textarea>``
|
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``CheckboxInput``
|
|
|
|
~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. class:: CheckboxInput
|
2008-08-27 05:56:57 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Checkbox: ``<input type='checkbox' ...>``
|
|
|
|
|
2009-08-29 12:40:47 +00:00
|
|
|
Takes one optional argument:
|
|
|
|
|
|
|
|
.. attribute:: CheckboxInput.check_test
|
2010-08-06 14:25:58 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
A callable that takes the value of the CheckBoxInput and returns
|
|
|
|
``True`` if the checkbox should be checked for that value.
|
2009-08-29 12:40:47 +00:00
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``Select``
|
|
|
|
~~~~~~~~~~
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. class:: Select
|
2008-08-27 05:56:57 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Select widget: ``<select><option ...>...</select>``
|
2010-08-06 14:25:58 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
.. attribute:: Select.choices
|
|
|
|
|
|
|
|
This attribute is optional when the field does not have a
|
|
|
|
:attr:`~Field.choices` attribute. If it does, it will override anything
|
|
|
|
you set here when the attribute is updated on the :class:`Field`.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``NullBooleanSelect``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. class:: NullBooleanSelect
|
2008-08-27 05:56:57 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Select widget with options 'Unknown', 'Yes' and 'No'
|
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``SelectMultiple``
|
|
|
|
~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. class:: SelectMultiple
|
2008-08-27 05:56:57 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
Similar to :class:`Select`, but allows multiple selection:
|
|
|
|
``<select multiple='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-03 18:30:54 +00:00
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``RadioSelect``
|
|
|
|
~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. class:: RadioSelect
|
2008-08-27 05:56:57 +00:00
|
|
|
|
2011-12-07 22:31:39 +00:00
|
|
|
Similar to :class:`Select`, but rendered as a list of radio buttons within
|
|
|
|
``<li>`` tags:
|
2010-08-06 14:25:58 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. code-block:: html
|
2010-08-06 14:25:58 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
<ul>
|
|
|
|
<li><input type='radio' ...></li>
|
|
|
|
...
|
|
|
|
</ul>
|
2010-08-06 14:25:58 +00:00
|
|
|
|
2011-12-07 22:31:39 +00:00
|
|
|
.. versionadded:: 1.4
|
|
|
|
|
|
|
|
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">
|
|
|
|
<label><input type="radio" name="beatles" value="john" /> John</label>
|
|
|
|
</div>
|
|
|
|
<div class="myradio">
|
|
|
|
<label><input type="radio" name="beatles" value="paul" /> Paul</label>
|
|
|
|
</div>
|
|
|
|
<div class="myradio">
|
|
|
|
<label><input type="radio" name="beatles" value="george" /> George</label>
|
|
|
|
</div>
|
|
|
|
<div class="myradio">
|
|
|
|
<label><input type="radio" name="beatles" value="ringo" /> Ringo</label>
|
|
|
|
</div>
|
|
|
|
|
2011-12-07 23:08:27 +00:00
|
|
|
That included the ``<label>`` tags. To get more granular, you can use each
|
|
|
|
radio button's ``tag`` and ``choice_label`` attributes. For example, this template...
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
{% for radio in myform.beatles %}
|
|
|
|
<label>
|
|
|
|
{{ radio.choice_label }}
|
|
|
|
<span class="radio">{{ radio.tag }}</span>
|
|
|
|
</label>
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
...will result in the following HTML:
|
|
|
|
|
|
|
|
.. code-block:: html
|
|
|
|
|
|
|
|
<label>
|
|
|
|
John
|
|
|
|
<span class="radio"><input type="radio" name="beatles" value="john" /></span>
|
|
|
|
</label>
|
|
|
|
<label>
|
|
|
|
Paul
|
|
|
|
<span class="radio"><input type="radio" name="beatles" value="paul" /></span>
|
|
|
|
</label>
|
|
|
|
<label>
|
|
|
|
George
|
|
|
|
<span class="radio"><input type="radio" name="beatles" value="george" /></span>
|
|
|
|
</label>
|
|
|
|
<label>
|
|
|
|
Ringo
|
|
|
|
<span class="radio"><input type="radio" name="beatles" value="ringo" /></span>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
If you decide not to loop over the radio buttons -- e.g., if your template simply includes
|
|
|
|
``{{ myform.beatles }}`` -- they'll be output in a ``<ul>`` with ``<li>`` tags, as above.
|
2011-12-07 22:31:39 +00:00
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``CheckboxSelectMultiple``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. class:: CheckboxSelectMultiple
|
2008-08-27 05:56:57 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
Similar to :class:`SelectMultiple`, but rendered as a list of check
|
|
|
|
buttons:
|
2010-08-06 14:25:58 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. code-block:: html
|
2010-08-06 14:25:58 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
<ul>
|
|
|
|
<li><input type='checkbox' ...></li>
|
|
|
|
...
|
|
|
|
</ul>
|
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``MultiWidget``
|
|
|
|
~~~~~~~~~~~~~~~
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. class:: MultiWidget
|
2008-08-27 05:56:57 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
Wrapper around multiple other widgets. You'll probably want to use this
|
|
|
|
class with :class:`MultiValueField`.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
Its ``render()`` method is different than other widgets', because it has to
|
|
|
|
figure out how to split a single value for display in multiple widgets.
|
2009-03-22 16:13:06 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
Subclasses may implement ``format_output``, which takes the list of
|
|
|
|
rendered widgets and returns a string of HTML that formats them any way
|
|
|
|
you'd like.
|
2010-08-06 14:25:58 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
The ``value`` argument used when rendering can be one of two things:
|
2011-02-16 01:56:53 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
* A ``list``.
|
|
|
|
* A single value (e.g., a string) that is the "compressed" representation
|
|
|
|
of a ``list`` of values.
|
2009-09-10 15:45:15 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
In the second case -- i.e., if the value is *not* a list -- ``render()``
|
|
|
|
will first decompress the value into a ``list`` before rendering it. It
|
|
|
|
does so by calling the ``decompress()`` method, which
|
|
|
|
:class:`MultiWidget`'s subclasses must implement. This method takes a
|
|
|
|
single "compressed" value and returns a ``list``. An example of this is how
|
|
|
|
:class:`SplitDateTimeWidget` turns a :class:`datetime` value into a list
|
2011-10-14 00:12:01 +00:00
|
|
|
with date and time split into two seperate values::
|
2009-09-10 15:45:15 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
class SplitDateTimeWidget(MultiWidget):
|
2009-09-10 15:45:15 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
# ...
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
def decompress(self, value):
|
|
|
|
if value:
|
|
|
|
return [value.date(), value.time().replace(microsecond=0)]
|
|
|
|
return [None, None]
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
When ``render()`` executes its HTML rendering, each value in the list is
|
|
|
|
rendered with the corresponding widget -- the first value is rendered in
|
|
|
|
the first widget, the second value is rendered in the second widget, etc.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
:class:`MultiWidget` has one required argument:
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
.. attribute:: MultiWidget.widgets
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
An iterable containing the widgets needed.
|
2009-02-21 08:45:47 +00:00
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``SplitDateTimeWidget``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
.. class:: SplitDateTimeWidget
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
Wrapper (using :class:`MultiWidget`) around two widgets: :class:`DateInput`
|
|
|
|
for the date, and :class:`TimeInput` for the time.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
``SplitDateTimeWidget`` has two optional attributes:
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
.. attribute:: SplitDateTimeWidget.date_format
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
Similar to :attr:`DateInput.format`
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
.. attribute:: SplitDateTimeWidget.time_format
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
Similar to :attr:`TimeInput.format`
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``SplitHiddenDateTimeWidget``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
.. class:: SplitHiddenDateTimeWidget
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
Similar to :class:`SplitDateTimeWidget`, but uses :class:`HiddenInput` for
|
|
|
|
both date and time.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-07-09 12:19:29 +00:00
|
|
|
.. currentmodule:: django.forms.extras.widgets
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-08-21 11:51:48 +00:00
|
|
|
``SelectDateWidget``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
.. class:: SelectDateWidget
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
Wrapper around three :class:`~django.forms.Select` widgets: one each for
|
|
|
|
month, day, and year. Note that this widget lives in a separate file from
|
|
|
|
the standard widgets.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
Takes one optional argument:
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00:00
|
|
|
.. attribute:: SelectDateWidget.years
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-16 15:27:19 +00: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.
|