widgets.txt 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. Django provides a representation of all the basic HTML widgets, plus some
  11. commonly used groups of widgets:
  12. .. class:: TextInput
  13. Text input: ``<input type='text' ...>``
  14. .. class:: PasswordInput
  15. Password input: ``<input type='password' ...>``
  16. Takes one optional argument:
  17. .. attribute:: PasswordInput.render_value
  18. Determines whether the widget will have a value filled in when the
  19. form is re-displayed after a validation error (default is ``False``).
  20. .. versionchanged:: 1.3
  21. The default value for
  22. :attr:`~PasswordInput.render_value` was
  23. changed from ``True`` to ``False``
  24. .. class:: HiddenInput
  25. Hidden input: ``<input type='hidden' ...>``
  26. .. class:: MultipleHiddenInput
  27. Multiple ``<input type='hidden' ...>`` widgets.
  28. .. class:: FileInput
  29. File upload input: ``<input type='file' ...>``
  30. .. class:: ClearableFileInput
  31. .. versionadded:: 1.3
  32. File upload input: ``<input type='file' ...>``, with an additional checkbox
  33. input to clear the field's value, if the field is not required and has
  34. initial data.
  35. .. class:: DateInput
  36. Date input as a simple text box: ``<input type='text' ...>``
  37. Takes one optional argument:
  38. .. attribute:: DateInput.format
  39. The format in which this field's initial value will be displayed.
  40. If no ``format`` argument is provided, the default format is ``'%Y-%m-%d'``.
  41. .. class:: DateTimeInput
  42. Date/time input as a simple text box: ``<input type='text' ...>``
  43. Takes one optional argument:
  44. .. attribute:: DateTimeInput.format
  45. The format in which this field's initial value will be displayed.
  46. If no ``format`` argument is provided, the default format is ``'%Y-%m-%d
  47. %H:%M:%S'``.
  48. .. class:: TimeInput
  49. Time input as a simple text box: ``<input type='text' ...>``
  50. Takes one optional argument:
  51. .. attribute:: TimeInput.format
  52. The format in which this field's initial value will be displayed.
  53. If no ``format`` argument is provided, the default format is ``'%H:%M:%S'``.
  54. .. class:: Textarea
  55. Text area: ``<textarea>...</textarea>``
  56. .. class:: CheckboxInput
  57. Checkbox: ``<input type='checkbox' ...>``
  58. Takes one optional argument:
  59. .. attribute:: CheckboxInput.check_test
  60. A callable that takes the value of the CheckBoxInput
  61. and returns ``True`` if the checkbox should be checked for
  62. that value.
  63. .. class:: Select
  64. Select widget: ``<select><option ...>...</select>``
  65. Requires that your field provides :attr:`~Field.choices`.
  66. .. class:: NullBooleanSelect
  67. Select widget with options 'Unknown', 'Yes' and 'No'
  68. .. class:: SelectMultiple
  69. Select widget allowing multiple selection: ``<select
  70. multiple='multiple'>...</select>``
  71. Requires that your field provides :attr:`~Field.choices`.
  72. .. class:: RadioSelect
  73. A list of radio buttons:
  74. .. code-block:: html
  75. <ul>
  76. <li><input type='radio' ...></li>
  77. ...
  78. </ul>
  79. Requires that your field provides :attr:`~Field.choices`.
  80. .. class:: CheckboxSelectMultiple
  81. A list of checkboxes:
  82. .. code-block:: html
  83. <ul>
  84. <li><input type='checkbox' ...></li>
  85. ...
  86. </ul>
  87. .. class:: MultiWidget
  88. Wrapper around multiple other widgets
  89. .. class:: SplitDateTimeWidget
  90. Wrapper around two widgets: ``DateInput`` for the date, and ``TimeInput``
  91. for the time.
  92. Takes two optional arguments, ``date_format`` and ``time_format``, which
  93. work just like the ``format`` argument for ``DateInput`` and ``TimeInput``.
  94. .. currentmodule:: django.forms.extras.widgets
  95. .. class:: SelectDateWidget
  96. Wrapper around three select widgets: one each for month, day, and year.
  97. Note that this widget lives in a separate file from the standard widgets.
  98. Takes one optional argument:
  99. .. attribute:: List.years
  100. An optional list/tuple of years to use in the "year" select box.
  101. The default is a list containing the current year and the next 9 years.
  102. .. code-block:: python
  103. from django.forms.extras.widgets import SelectDateWidget
  104. date = forms.DateField(widget=SelectDateWidget())
  105. Specifying widgets
  106. ------------------
  107. .. currentmodule:: django.forms
  108. .. attribute:: Form.widget
  109. Whenever you specify a field on a form, Django will use a default widget
  110. that is appropriate to the type of data that is to be displayed. To find
  111. which widget is used on which field, see the documentation for the
  112. built-in Field classes.
  113. However, if you want to use a different widget for a field, you can -
  114. just use the 'widget' argument on the field definition. For example::
  115. from django import forms
  116. class CommentForm(forms.Form):
  117. name = forms.CharField()
  118. url = forms.URLField()
  119. comment = forms.CharField(widget=forms.Textarea)
  120. This would specify a form with a comment that uses a larger Textarea widget,
  121. rather than the default TextInput widget.
  122. Customizing widget instances
  123. ----------------------------
  124. When Django renders a widget as HTML, it only renders the bare minimum
  125. HTML - Django doesn't add a class definition, or any other widget-specific
  126. attributes. This means that all 'TextInput' widgets will appear the same
  127. on your Web page.
  128. If you want to make one widget look different to another, you need to
  129. specify additional attributes for each widget. When you specify a
  130. widget, you can provide a list of attributes that will be added to the
  131. rendered HTML for the widget.
  132. For example, take the following simple form::
  133. class CommentForm(forms.Form):
  134. name = forms.CharField()
  135. url = forms.URLField()
  136. comment = forms.CharField()
  137. This form will include three default TextInput widgets, with default rendering -
  138. no CSS class, no extra attributes. This means that the input boxes provided for
  139. each widget will be rendered exactly the same::
  140. >>> f = CommentForm(auto_id=False)
  141. >>> f.as_table()
  142. <tr><th>Name:</th><td><input type="text" name="name" /></td></tr>
  143. <tr><th>Url:</th><td><input type="text" name="url"/></td></tr>
  144. <tr><th>Comment:</th><td><input type="text" name="comment" /></td></tr>
  145. On a real Web page, you probably don't want every widget to look the same. You
  146. might want a larger input element for the comment, and you might want the 'name'
  147. widget to have some special CSS class. To do this, you use the ``attrs``
  148. argument when creating the widget:
  149. .. attribute:: Widget.attrs
  150. For example::
  151. class CommentForm(forms.Form):
  152. name = forms.CharField(
  153. widget=forms.TextInput(attrs={'class':'special'}))
  154. url = forms.URLField()
  155. comment = forms.CharField(
  156. widget=forms.TextInput(attrs={'size':'40'}))
  157. Django will then include the extra attributes in the rendered output::
  158. >>> f = CommentForm(auto_id=False)
  159. >>> f.as_table()
  160. <tr><th>Name:</th><td><input type="text" name="name" class="special"/></td></tr>
  161. <tr><th>Url:</th><td><input type="text" name="url"/></td></tr>
  162. <tr><th>Comment:</th><td><input type="text" name="comment" size="40"/></td></tr>