widgets.txt 6.2 KB

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