widgets.txt 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. =======
  2. Widgets
  3. =======
  4. .. module:: django.forms.widgets
  5. :synopsis: Django's built-in form widgets.
  6. .. currentmodule:: django.forms
  7. A widget is Django's representation of a HTML input element. The widget
  8. handles the rendering of the HTML, and the extraction of data from a GET/POST
  9. dictionary that corresponds to the widget.
  10. .. tip::
  11. Widgets should not be confused with the :doc:`form fields </ref/forms/fields>`.
  12. Form fields deal with the logic of input validation and are used directly
  13. in templates. Widgets deal with rendering of HTML form input elements on
  14. the web page and extraction of raw submitted data. However, widgets do
  15. need to be :ref:`assigned <widget-to-field>` to form fields.
  16. .. _widget-to-field:
  17. Specifying widgets
  18. ------------------
  19. Whenever you specify a field on a form, Django will use a default widget
  20. that is appropriate to the type of data that is to be displayed. To find
  21. which widget is used on which field, see the documentation about
  22. :ref:`built-in fields`.
  23. However, if you want to use a different widget for a field, you can
  24. just use the :attr:`~Field.widget` argument on the field definition. For
  25. example::
  26. from django import forms
  27. class CommentForm(forms.Form):
  28. name = forms.CharField()
  29. url = forms.URLField()
  30. comment = forms.CharField(widget=forms.Textarea)
  31. This would specify a form with a comment that uses a larger :class:`Textarea`
  32. widget, rather than the default :class:`TextInput` widget.
  33. Setting arguments for widgets
  34. -----------------------------
  35. Many widgets have optional extra arguments; they can be set when defining the
  36. widget on the field. In the following example, the
  37. :attr:`~django.forms.extras.widgets.SelectDateWidget.years` attribute is set
  38. for a :class:`~django.forms.extras.widgets.SelectDateWidget`::
  39. from django import forms
  40. from django.forms.extras.widgets import SelectDateWidget
  41. BIRTH_YEAR_CHOICES = ('1980', '1981', '1982')
  42. FAVORITE_COLORS_CHOICES = (('blue', 'Blue'),
  43. ('green', 'Green'),
  44. ('black', 'Black'))
  45. class SimpleForm(forms.Form):
  46. birth_year = forms.DateField(widget=SelectDateWidget(years=BIRTH_YEAR_CHOICES))
  47. favorite_colors = forms.MultipleChoiceField(required=False,
  48. widget=forms.CheckboxSelectMultiple, choices=FAVORITE_COLORS_CHOICES)
  49. See the :ref:`built-in widgets` for more information about which widgets
  50. are available and which arguments they accept.
  51. Widgets inheriting from the Select widget
  52. -----------------------------------------
  53. Widgets inheriting from the :class:`Select` widget deal with choices. They
  54. present the user with a list of options to choose from. The different widgets
  55. present this choice differently; the :class:`Select` widget itself uses a
  56. ``<select>`` HTML list representation, while :class:`RadioSelect` uses radio
  57. buttons.
  58. :class:`Select` widgets are used by default on :class:`ChoiceField` fields. The
  59. choices displayed on the widget are inherited from the :class:`ChoiceField` and
  60. changing :attr:`ChoiceField.choices` will update :attr:`Select.choices`. For
  61. example::
  62. >>> from django import forms
  63. >>> CHOICES = (('1', 'First',), ('2', 'Second',))
  64. >>> choice_field = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES)
  65. >>> choice_field.choices
  66. [('1', 'First'), ('2', 'Second')]
  67. >>> choice_field.widget.choices
  68. [('1', 'First'), ('2', 'Second')]
  69. >>> choice_field.widget.choices = ()
  70. >>> choice_field.choices = (('1', 'First and only',),)
  71. >>> choice_field.widget.choices
  72. [('1', 'First and only')]
  73. Widgets which offer a :attr:`~Select.choices` attribute can however be used
  74. with fields which are not based on choice -- such as a :class:`CharField` --
  75. but it is recommended to use a :class:`ChoiceField`-based field when the
  76. choices are inherent to the model and not just the representational widget.
  77. Customizing widget instances
  78. ----------------------------
  79. When Django renders a widget as HTML, it only renders very minimal markup -
  80. Django doesn't add class names, or any other widget-specific attributes. This
  81. means, for example, that all :class:`TextInput` widgets will appear the same
  82. on your Web pages.
  83. There are two ways to customize widgets: :ref:`per widget instance
  84. <styling-widget-instances>` and :ref:`per widget class <styling-widget-classes>`.
  85. .. _styling-widget-instances:
  86. Styling widget instances
  87. ^^^^^^^^^^^^^^^^^^^^^^^^
  88. If you want to make one widget instance look different from another, you will
  89. need to specify additional attributes at the time when the widget object is
  90. instantiated and assigned to a form field (and perhaps add some rules to your
  91. CSS files).
  92. For example, take the following simple form::
  93. from django import forms
  94. class CommentForm(forms.Form):
  95. name = forms.CharField()
  96. url = forms.URLField()
  97. comment = forms.CharField()
  98. This form will include three default :class:`TextInput` widgets, with default
  99. rendering -- no CSS class, no extra attributes. This means that the input boxes
  100. provided for each widget will be rendered exactly the same::
  101. >>> f = CommentForm(auto_id=False)
  102. >>> f.as_table()
  103. <tr><th>Name:</th><td><input type="text" name="name" /></td></tr>
  104. <tr><th>Url:</th><td><input type="url" name="url"/></td></tr>
  105. <tr><th>Comment:</th><td><input type="text" name="comment" /></td></tr>
  106. On a real Web page, you probably don't want every widget to look the same. You
  107. might want a larger input element for the comment, and you might want the
  108. 'name' widget to have some special CSS class. It is also possible to specify
  109. the 'type' attribute to take advantage of the new HTML5 input types. To do
  110. this, you use the :attr:`Widget.attrs` argument when creating the widget::
  111. class CommentForm(forms.Form):
  112. name = forms.CharField(
  113. widget=forms.TextInput(attrs={'class':'special'}))
  114. url = forms.URLField()
  115. comment = forms.CharField(
  116. widget=forms.TextInput(attrs={'size':'40'}))
  117. Django will then include the extra attributes in the rendered output:
  118. >>> f = CommentForm(auto_id=False)
  119. >>> f.as_table()
  120. <tr><th>Name:</th><td><input type="text" name="name" class="special"/></td></tr>
  121. <tr><th>Url:</th><td><input type="url" name="url"/></td></tr>
  122. <tr><th>Comment:</th><td><input type="text" name="comment" size="40"/></td></tr>
  123. You can also set the HTML ``id`` using :attr:`~Widget.attrs`. See
  124. :attr:`BoundField.id_for_label` for an example.
  125. .. _styling-widget-classes:
  126. Styling widget classes
  127. ^^^^^^^^^^^^^^^^^^^^^^
  128. With widgets, it is possible to add assets (``css`` and ``javascript``)
  129. and more deeply customize their appearance and behavior.
  130. In a nutshell, you will need to subclass the widget and either
  131. :ref:`define a "Media" inner class <assets-as-a-static-definition>` or
  132. :ref:`create a "media" property <dynamic-property>`.
  133. These methods involve somewhat advanced Python programming and are described in
  134. detail in the :doc:`Form Assets </topics/forms/media>` topic guide.
  135. .. _base-widget-classes:
  136. Base Widget classes
  137. -------------------
  138. Base widget classes :class:`Widget` and :class:`MultiWidget` are subclassed by
  139. all the :ref:`built-in widgets <built-in widgets>` and may serve as a
  140. foundation for custom widgets.
  141. .. class:: Widget(attrs=None)
  142. This abstract class cannot be rendered, but provides the basic attribute
  143. :attr:`~Widget.attrs`. You may also implement or override the
  144. :meth:`~Widget.render()` method on custom widgets.
  145. .. attribute:: Widget.attrs
  146. A dictionary containing HTML attributes to be set on the rendered
  147. widget.
  148. .. code-block:: python
  149. >>> from django import forms
  150. >>> name = forms.TextInput(attrs={'size': 10, 'title': 'Your name',})
  151. >>> name.render('name', 'A name')
  152. u'<input title="Your name" type="text" name="name" value="A name" size="10" />'
  153. .. method:: render(name, value, attrs=None)
  154. Returns HTML for the widget, as a Unicode string. This method must be
  155. implemented by the subclass, otherwise ``NotImplementedError`` will be
  156. raised.
  157. The 'value' given is not guaranteed to be valid input, therefore
  158. subclass implementations should program defensively.
  159. .. method:: value_from_datadict(self, data, files, name)
  160. Given a dictionary of data and this widget's name, returns the value
  161. of this widget. Returns ``None`` if a value wasn't provided.
  162. .. class:: MultiWidget(widgets, attrs=None)
  163. A widget that is composed of multiple widgets.
  164. :class:`~django.forms.MultiWidget` works hand in hand with the
  165. :class:`~django.forms.MultiValueField`.
  166. :class:`MultiWidget` has one required argument:
  167. .. attribute:: MultiWidget.widgets
  168. An iterable containing the widgets needed.
  169. And one required method:
  170. .. method:: decompress(value)
  171. This method takes a single "compressed" value from the field and
  172. returns a list of "decompressed" values. The input value can be
  173. assumed valid, but not necessarily non-empty.
  174. This method **must be implemented** by the subclass, and since the
  175. value may be empty, the implementation must be defensive.
  176. The rationale behind "decompression" is that it is necessary to "split"
  177. the combined value of the form field into the values for each widget.
  178. An example of this is how :class:`SplitDateTimeWidget` turns a
  179. :class:`~datetime.datetime` value into a list with date and time split
  180. into two separate values::
  181. from django.forms import MultiWidget
  182. class SplitDateTimeWidget(MultiWidget):
  183. # ...
  184. def decompress(self, value):
  185. if value:
  186. return [value.date(), value.time().replace(microsecond=0)]
  187. return [None, None]
  188. .. tip::
  189. Note that :class:`~django.forms.MultiValueField` has a
  190. complementary method :meth:`~django.forms.MultiValueField.compress`
  191. with the opposite responsibility - to combine cleaned values of
  192. all member fields into one.
  193. Other methods that may be useful to override include:
  194. .. method:: render(name, value, attrs=None)
  195. Argument ``value`` is handled differently in this method from the
  196. subclasses of :class:`~Widget` because it has to figure out how to
  197. split a single value for display in multiple widgets.
  198. The ``value`` argument used when rendering can be one of two things:
  199. * A ``list``.
  200. * A single value (e.g., a string) that is the "compressed" representation
  201. of a ``list`` of values.
  202. If ``value`` is a list, the output of :meth:`~MultiWidget.render` will
  203. be a concatenation of rendered child widgets. If ``value`` is not a
  204. list, it will first be processed by the method
  205. :meth:`~MultiWidget.decompress()` to create the list and then rendered.
  206. When ``render()`` executes its HTML rendering, each value in the list
  207. is rendered with the corresponding widget -- the first value is
  208. rendered in the first widget, the second value is rendered in the
  209. second widget, etc.
  210. Unlike in the single value widgets, method :meth:`~MultiWidget.render`
  211. need not be implemented in the subclasses.
  212. .. method:: format_output(rendered_widgets)
  213. Given a list of rendered widgets (as strings), returns a Unicode string
  214. representing the HTML for the whole lot.
  215. This hook allows you to format the HTML design of the widgets any way
  216. you'd like.
  217. Here's an example widget which subclasses :class:`MultiWidget` to display
  218. a date with the day, month, and year in different select boxes. This widget
  219. is intended to be used with a :class:`~django.forms.DateField` rather than
  220. a :class:`~django.forms.MultiValueField`, thus we have implemented
  221. :meth:`~Widget.value_from_datadict`::
  222. from datetime import date
  223. from django.forms import widgets
  224. class DateSelectorWidget(widgets.MultiWidget):
  225. def __init__(self, attrs=None):
  226. # create choices for days, months, years
  227. # example below, the rest snipped for brevity.
  228. years = [(year, year) for year in (2011, 2012, 2013)]
  229. _widgets = (
  230. widgets.Select(attrs=attrs, choices=days),
  231. widgets.Select(attrs=attrs, choices=months),
  232. widgets.Select(attrs=attrs, choices=years),
  233. )
  234. super(DateSelectorWidget, self).__init__(_widgets, attrs)
  235. def decompress(self, value):
  236. if value:
  237. return [value.day, value.month, value.year]
  238. return [None, None, None]
  239. def format_output(self, rendered_widgets):
  240. return u''.join(rendered_widgets)
  241. def value_from_datadict(self, data, files, name):
  242. datelist = [
  243. widget.value_from_datadict(data, files, name + '_%s' % i)
  244. for i, widget in enumerate(self.widgets)]
  245. try:
  246. D = date(day=int(datelist[0]), month=int(datelist[1]),
  247. year=int(datelist[2]))
  248. except ValueError:
  249. return ''
  250. else:
  251. return str(D)
  252. The constructor creates several :class:`Select` widgets in a tuple. The
  253. ``super`` class uses this tuple to setup the widget.
  254. The :meth:`~MultiWidget.format_output` method is fairly vanilla here (in
  255. fact, it's the same as what's been implemented as the default for
  256. ``MultiWidget``), but the idea is that you could add custom HTML between
  257. the widgets should you wish.
  258. The required method :meth:`~MultiWidget.decompress` breaks up a
  259. ``datetime.date`` value into the day, month, and year values corresponding
  260. to each widget. Note how the method handles the case where ``value`` is
  261. ``None``.
  262. The default implementation of :meth:`~Widget.value_from_datadict` returns
  263. a list of values corresponding to each ``Widget``. This is appropriate
  264. when using a ``MultiWidget`` with a :class:`~django.forms.MultiValueField`,
  265. but since we want to use this widget with a :class:`~django.forms.DateField`
  266. which takes a single value, we have overridden this method to combine the
  267. data of all the subwidgets into a ``datetime.date``. The method extracts
  268. data from the ``POST`` dictionary and constructs and validates the date.
  269. If it is valid, we return the string, otherwise, we return an empty string
  270. which will cause ``form.is_valid`` to return ``False``.
  271. .. _built-in widgets:
  272. Built-in widgets
  273. ----------------
  274. Django provides a representation of all the basic HTML widgets, plus some
  275. commonly used groups of widgets in the ``django.forms.widgets`` module,
  276. including :ref:`the input of text <text-widgets>`, :ref:`various checkboxes
  277. and selectors <selector-widgets>`, :ref:`uploading files <file-upload-widgets>`,
  278. and :ref:`handling of multi-valued input <composite-widgets>`.
  279. .. _text-widgets:
  280. Widgets handling input of text
  281. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  282. These widgets make use of the HTML elements ``input`` and ``textarea``.
  283. ``TextInput``
  284. ~~~~~~~~~~~~~
  285. .. class:: TextInput
  286. Text input: ``<input type="text" ...>``
  287. ``NumberInput``
  288. ~~~~~~~~~~~~~~~
  289. .. class:: NumberInput
  290. .. versionadded:: 1.6
  291. Text input: ``<input type="number" ...>``
  292. Beware that not all browsers support entering localized numbers in
  293. ``number`` input types. Django itself avoids using them for fields having
  294. their :attr:`~django.forms.Field.localize` property to ``True``.
  295. ``EmailInput``
  296. ~~~~~~~~~~~~~~
  297. .. class:: EmailInput
  298. .. versionadded:: 1.6
  299. Text input: ``<input type="email" ...>``
  300. ``URLInput``
  301. ~~~~~~~~~~~~
  302. .. class:: URLInput
  303. .. versionadded:: 1.6
  304. Text input: ``<input type="url" ...>``
  305. ``PasswordInput``
  306. ~~~~~~~~~~~~~~~~~
  307. .. class:: PasswordInput
  308. Password input: ``<input type='password' ...>``
  309. Takes one optional argument:
  310. .. attribute:: PasswordInput.render_value
  311. Determines whether the widget will have a value filled in when the
  312. form is re-displayed after a validation error (default is ``False``).
  313. ``HiddenInput``
  314. ~~~~~~~~~~~~~~~
  315. .. class:: HiddenInput
  316. Hidden input: ``<input type='hidden' ...>``
  317. Note that there also is a :class:`MultipleHiddenInput` widget that
  318. encapsulates a set of hidden input elements.
  319. ``DateInput``
  320. ~~~~~~~~~~~~~
  321. .. class:: DateInput
  322. Date input as a simple text box: ``<input type='text' ...>``
  323. Takes same arguments as :class:`TextInput`, with one more optional argument:
  324. .. attribute:: DateInput.format
  325. The format in which this field's initial value will be displayed.
  326. If no ``format`` argument is provided, the default format is the first
  327. format found in :setting:`DATE_INPUT_FORMATS` and respects
  328. :ref:`format-localization`.
  329. ``DateTimeInput``
  330. ~~~~~~~~~~~~~~~~~
  331. .. class:: DateTimeInput
  332. Date/time input as a simple text box: ``<input type='text' ...>``
  333. Takes same arguments as :class:`TextInput`, with one more optional argument:
  334. .. attribute:: DateTimeInput.format
  335. The format in which this field's initial value will be displayed.
  336. If no ``format`` argument is provided, the default format is the first
  337. format found in :setting:`DATETIME_INPUT_FORMATS` and respects
  338. :ref:`format-localization`.
  339. ``TimeInput``
  340. ~~~~~~~~~~~~~
  341. .. class:: TimeInput
  342. Time input as a simple text box: ``<input type='text' ...>``
  343. Takes same arguments as :class:`TextInput`, with one more optional argument:
  344. .. attribute:: TimeInput.format
  345. The format in which this field's initial value will be displayed.
  346. If no ``format`` argument is provided, the default format is the first
  347. format found in :setting:`TIME_INPUT_FORMATS` and respects
  348. :ref:`format-localization`.
  349. ``Textarea``
  350. ~~~~~~~~~~~~
  351. .. class:: Textarea
  352. Text area: ``<textarea>...</textarea>``
  353. .. _selector-widgets:
  354. Selector and checkbox widgets
  355. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  356. ``CheckboxInput``
  357. ~~~~~~~~~~~~~~~~~
  358. .. class:: CheckboxInput
  359. Checkbox: ``<input type='checkbox' ...>``
  360. Takes one optional argument:
  361. .. attribute:: CheckboxInput.check_test
  362. A callable that takes the value of the CheckBoxInput and returns
  363. ``True`` if the checkbox should be checked for that value.
  364. ``Select``
  365. ~~~~~~~~~~
  366. .. class:: Select
  367. Select widget: ``<select><option ...>...</select>``
  368. .. attribute:: Select.choices
  369. This attribute is optional when the form field does not have a
  370. ``choices`` attribute. If it does, it will override anything you set
  371. here when the attribute is updated on the :class:`Field`.
  372. ``NullBooleanSelect``
  373. ~~~~~~~~~~~~~~~~~~~~~
  374. .. class:: NullBooleanSelect
  375. Select widget with options 'Unknown', 'Yes' and 'No'
  376. ``SelectMultiple``
  377. ~~~~~~~~~~~~~~~~~~
  378. .. class:: SelectMultiple
  379. Similar to :class:`Select`, but allows multiple selection:
  380. ``<select multiple='multiple'>...</select>``
  381. ``RadioSelect``
  382. ~~~~~~~~~~~~~~~
  383. .. class:: RadioSelect
  384. Similar to :class:`Select`, but rendered as a list of radio buttons within
  385. ``<li>`` tags:
  386. .. code-block:: html
  387. <ul>
  388. <li><input type='radio' ...></li>
  389. ...
  390. </ul>
  391. For more granular control over the generated markup, you can loop over the
  392. radio buttons in the template. Assuming a form ``myform`` with a field
  393. ``beatles`` that uses a ``RadioSelect`` as its widget:
  394. .. code-block:: html+django
  395. {% for radio in myform.beatles %}
  396. <div class="myradio">
  397. {{ radio }}
  398. </div>
  399. {% endfor %}
  400. This would generate the following HTML:
  401. .. code-block:: html
  402. <div class="myradio">
  403. <label for="id_beatles_0"><input id="id_beatles_0" name="beatles" type="radio" value="john" /> John</label>
  404. </div>
  405. <div class="myradio">
  406. <label for="id_beatles_1"><input id="id_beatles_1" name="beatles" type="radio" value="paul" /> Paul</label>
  407. </div>
  408. <div class="myradio">
  409. <label for="id_beatles_2"><input id="id_beatles_2" name="beatles" type="radio" value="george" /> George</label>
  410. </div>
  411. <div class="myradio">
  412. <label for="id_beatles_3"><input id="id_beatles_3" name="beatles" type="radio" value="ringo" /> Ringo</label>
  413. </div>
  414. That included the ``<label>`` tags. To get more granular, you can use each
  415. radio button's ``tag``, ``choice_label`` and ``id_for_label`` attributes.
  416. For example, this template...
  417. .. code-block:: html+django
  418. {% for radio in myform.beatles %}
  419. <label for="{{ radio.id_for_label }}">
  420. {{ radio.choice_label }}
  421. <span class="radio">{{ radio.tag }}</span>
  422. </label>
  423. {% endfor %}
  424. ...will result in the following HTML:
  425. .. code-block:: html
  426. <label for="id_beatles_0">
  427. John
  428. <span class="radio"><input id="id_beatles_0" name="beatles" type="radio" value="john" /></span>
  429. </label>
  430. <label for="id_beatles_1">
  431. Paul
  432. <span class="radio"><input id="id_beatles_1" name="beatles" type="radio" value="paul" /></span>
  433. </label>
  434. <label for="id_beatles_2">
  435. George
  436. <span class="radio"><input id="id_beatles_2" name="beatles" type="radio" value="george" /></span>
  437. </label>
  438. <label for="id_beatles_3">
  439. Ringo
  440. <span class="radio"><input id="id_beatles_3" name="beatles" type="radio" value="ringo" /></span>
  441. </label>
  442. If you decide not to loop over the radio buttons -- e.g., if your template
  443. simply includes ``{{ myform.beatles }}`` -- they'll be output in a ``<ul>``
  444. with ``<li>`` tags, as above.
  445. .. versionchanged:: 1.6
  446. The outer ``<ul>`` container will now receive the ``id`` attribute defined on
  447. the widget.
  448. .. versionchanged:: 1.7
  449. When looping over the radio buttons, the ``label`` and ``input`` tags include
  450. ``for`` and ``id`` attributes, respectively. Each radio button has an
  451. ``id_for_label`` attribute to output the element's ID.
  452. ``CheckboxSelectMultiple``
  453. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  454. .. class:: CheckboxSelectMultiple
  455. Similar to :class:`SelectMultiple`, but rendered as a list of check
  456. buttons:
  457. .. code-block:: html
  458. <ul>
  459. <li><input type='checkbox' ...></li>
  460. ...
  461. </ul>
  462. .. versionchanged:: 1.6
  463. The outer ``<ul>`` container will now receive the ``id`` attribute defined on
  464. the widget.
  465. Like :class:`RadioSelect`, you can now loop over the individual checkboxes making
  466. up the lists. See the documentation of :class:`RadioSelect` for more details.
  467. .. versionchanged:: 1.7
  468. When looping over the checkboxes, the ``label`` and ``input`` tags include
  469. ``for`` and ``id`` attributes, respectively. Each checkbox has an
  470. ``id_for_label`` attribute to output the element's ID.
  471. .. _file-upload-widgets:
  472. File upload widgets
  473. ^^^^^^^^^^^^^^^^^^^
  474. ``FileInput``
  475. ~~~~~~~~~~~~~
  476. .. class:: FileInput
  477. File upload input: ``<input type='file' ...>``
  478. ``ClearableFileInput``
  479. ~~~~~~~~~~~~~~~~~~~~~~
  480. .. class:: ClearableFileInput
  481. File upload input: ``<input type='file' ...>``, with an additional checkbox
  482. input to clear the field's value, if the field is not required and has
  483. initial data.
  484. .. _composite-widgets:
  485. Composite widgets
  486. ^^^^^^^^^^^^^^^^^
  487. ``MultipleHiddenInput``
  488. ~~~~~~~~~~~~~~~~~~~~~~~
  489. .. class:: MultipleHiddenInput
  490. Multiple ``<input type='hidden' ...>`` widgets.
  491. A widget that handles multiple hidden widgets for fields that have a list
  492. of values.
  493. .. attribute:: MultipleHiddenInput.choices
  494. This attribute is optional when the form field does not have a
  495. ``choices`` attribute. If it does, it will override anything you set
  496. here when the attribute is updated on the :class:`Field`.
  497. ``SplitDateTimeWidget``
  498. ~~~~~~~~~~~~~~~~~~~~~~~
  499. .. class:: SplitDateTimeWidget
  500. Wrapper (using :class:`MultiWidget`) around two widgets: :class:`DateInput`
  501. for the date, and :class:`TimeInput` for the time.
  502. ``SplitDateTimeWidget`` has two optional attributes:
  503. .. attribute:: SplitDateTimeWidget.date_format
  504. Similar to :attr:`DateInput.format`
  505. .. attribute:: SplitDateTimeWidget.time_format
  506. Similar to :attr:`TimeInput.format`
  507. ``SplitHiddenDateTimeWidget``
  508. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  509. .. class:: SplitHiddenDateTimeWidget
  510. Similar to :class:`SplitDateTimeWidget`, but uses :class:`HiddenInput` for
  511. both date and time.
  512. .. currentmodule:: django.forms.extras.widgets
  513. ``SelectDateWidget``
  514. ~~~~~~~~~~~~~~~~~~~~
  515. .. class:: SelectDateWidget
  516. Wrapper around three :class:`~django.forms.Select` widgets: one each for
  517. month, day, and year. Note that this widget lives in a separate file from
  518. the standard widgets.
  519. Takes one optional argument:
  520. .. attribute:: SelectDateWidget.years
  521. An optional list/tuple of years to use in the "year" select box.
  522. The default is a list containing the current year and the next 9 years.