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. .. versionadded:: 1.1
  37. Date input as a simple text box: ``<input type='text' ...>``
  38. Takes one optional argument:
  39. .. attribute:: DateInput.format
  40. The format in which this field's initial value will be displayed.
  41. If no ``format`` argument is provided, the default format is ``'%Y-%m-%d'``.
  42. .. class:: DateTimeInput
  43. .. versionadded:: 1.0
  44. Date/time input as a simple text box: ``<input type='text' ...>``
  45. Takes one optional argument:
  46. .. attribute:: DateTimeInput.format
  47. The format in which this field's initial value will be displayed.
  48. If no ``format`` argument is provided, the default format is ``'%Y-%m-%d
  49. %H:%M:%S'``.
  50. .. class:: TimeInput
  51. Time input as a simple text box: ``<input type='text' ...>``
  52. Takes one optional argument:
  53. .. attribute:: TimeInput.format
  54. The format in which this field's initial value will be displayed.
  55. If no ``format`` argument is provided, the default format is ``'%H:%M:%S'``.
  56. .. versionchanged:: 1.1
  57. The ``format`` argument was not supported in Django 1.0.
  58. .. class:: Textarea
  59. Text area: ``<textarea>...</textarea>``
  60. .. class:: CheckboxInput
  61. Checkbox: ``<input type='checkbox' ...>``
  62. Takes one optional argument:
  63. .. attribute:: CheckboxInput.check_test
  64. A callable that takes the value of the CheckBoxInput
  65. and returns ``True`` if the checkbox should be checked for
  66. that value.
  67. .. class:: Select
  68. Select widget: ``<select><option ...>...</select>``
  69. Requires that your field provides :attr:`~Field.choices`.
  70. .. class:: NullBooleanSelect
  71. Select widget with options 'Unknown', 'Yes' and 'No'
  72. .. class:: SelectMultiple
  73. Select widget allowing multiple selection: ``<select
  74. multiple='multiple'>...</select>``
  75. Requires that your field provides :attr:`~Field.choices`.
  76. .. class:: RadioSelect
  77. A list of radio buttons:
  78. .. code-block:: html
  79. <ul>
  80. <li><input type='radio' ...></li>
  81. ...
  82. </ul>
  83. Requires that your field provides :attr:`~Field.choices`.
  84. .. class:: CheckboxSelectMultiple
  85. A list of checkboxes:
  86. .. code-block:: html
  87. <ul>
  88. <li><input type='checkbox' ...></li>
  89. ...
  90. </ul>
  91. .. class:: MultiWidget
  92. Wrapper around multiple other widgets
  93. .. class:: SplitDateTimeWidget
  94. Wrapper around two widgets: ``DateInput`` for the date, and ``TimeInput``
  95. for the time.
  96. Takes two optional arguments, ``date_format`` and ``time_format``, which
  97. work just like the ``format`` argument for ``DateInput`` and ``TimeInput``.
  98. .. versionchanged:: 1.1
  99. The ``date_format`` and ``time_format`` arguments were not supported in Django 1.0.
  100. .. class:: SelectDateWidget
  101. Wrapper around three select widgets: one each for month, day, and year.
  102. Note that this widget lives in a separate file from the standard widgets.
  103. .. code-block:: python
  104. from django.forms.extras.widgets import SelectDateWidget
  105. date = forms.DateField(widget=SelectDateWidget())
  106. Specifying widgets
  107. ------------------
  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>