======= Widgets ======= .. module:: django.forms.widgets :synopsis: Django's built-in form widgets. .. 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. 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 example:: from django import forms class CommentForm(forms.Form): name = forms.CharField() url = forms.URLField() comment = forms.CharField(widget=forms.Textarea) 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 :class:`~django.forms.extras.widgets.SelectDateWidget`:: from django.forms.fields import DateField, ChoiceField, MultipleChoiceField from django.forms.widgets import RadioSelect, CheckboxSelectMultiple from django.forms.extras.widgets import SelectDateWidget BIRTH_YEAR_CHOICES = ('1980', '1981', '1982') FAVORITE_COLORS_CHOICES = (('blue', 'Blue'), ('green', 'Green'), ('black', 'Black')) class SimpleForm(forms.Form): birth_year = DateField(widget=SelectDateWidget(years=BIRTH_YEAR_CHOICES)) favorite_colors = forms.MultipleChoiceField(required=False, widget=CheckboxSelectMultiple, choices=FAVORITE_COLORS_CHOICES) 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 `` Url: Comment: 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. It is also possible to specify the 'type' attribute to take advantage of the new HTML5 input types. To do this, you use the :attr:`Widget.attrs` argument when creating the widget: For example:: 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'})) Django will then include the extra attributes in the rendered output: >>> f = CommentForm(auto_id=False) >>> f.as_table() Name: Url: Comment: .. _built-in widgets: Built-in widgets ---------------- Django provides a representation of all the basic HTML widgets, plus some commonly used groups of widgets: ``Widget`` ~~~~~~~~~~ .. 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'' ``TextInput`` ~~~~~~~~~~~~~ .. class:: TextInput Text input: ```` ``PasswordInput`` ~~~~~~~~~~~~~~~~~ .. class:: PasswordInput Password input: ```` Takes one optional argument: .. attribute:: PasswordInput.render_value Determines whether the widget will have a value filled in when the form is re-displayed after a validation error (default is ``False``). .. versionchanged:: 1.3 The default value for :attr:`~PasswordInput.render_value` was changed from ``True`` to ``False`` ``HiddenInput`` ~~~~~~~~~~~~~~~ .. class:: HiddenInput Hidden input: ```` ``MultipleHiddenInput`` ~~~~~~~~~~~~~~~~~~~~~~~ .. class:: MultipleHiddenInput Multiple ```` widgets. 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`. ``FileInput`` ~~~~~~~~~~~~~ .. class:: FileInput File upload input: ```` ``ClearableFileInput`` ~~~~~~~~~~~~~~~~~~~~~~ .. class:: ClearableFileInput .. versionadded:: 1.3 File upload input: ````, with an additional checkbox input to clear the field's value, if the field is not required and has initial data. ``DateInput`` ~~~~~~~~~~~~~ .. class:: DateInput Date input as a simple text box: ```` Takes same arguments as :class:`TextInput`, with one more optional argument: .. attribute:: DateInput.format The format in which this field's initial value will be displayed. If no ``format`` argument is provided, the default format is the first format found in :setting:`DATE_INPUT_FORMATS` and respects :ref:`format-localization`. ``DateTimeInput`` ~~~~~~~~~~~~~~~~~ .. class:: DateTimeInput Date/time input as a simple text box: ```` Takes same arguments as :class:`TextInput`, with one more optional argument: .. attribute:: DateTimeInput.format The format in which this field's initial value will be displayed. If no ``format`` argument is provided, the default format is the first format found in :setting:`DATETIME_INPUT_FORMATS` and respects :ref:`format-localization`. ``TimeInput`` ~~~~~~~~~~~~~ .. class:: TimeInput Time input as a simple text box: ```` Takes same arguments as :class:`TextInput`, with one more optional argument: .. attribute:: TimeInput.format The format in which this field's initial value will be displayed. If no ``format`` argument is provided, the default format is the first format found in :setting:`TIME_INPUT_FORMATS` and respects :ref:`format-localization`. ``Textarea`` ~~~~~~~~~~~~ .. class:: Textarea Text area: ```` ``CheckboxInput`` ~~~~~~~~~~~~~~~~~ .. class:: CheckboxInput Checkbox: ```` Takes one optional argument: .. attribute:: CheckboxInput.check_test A callable that takes the value of the CheckBoxInput and returns ``True`` if the checkbox should be checked for that value. .. versionchanged:: 1.5 Exceptions from ``check_test`` used to be silenced by its caller, this is no longer the case, they will propagate upwards. ``Select`` ~~~~~~~~~~ .. class:: Select Select widget: ```` .. 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`. ``NullBooleanSelect`` ~~~~~~~~~~~~~~~~~~~~~ .. class:: NullBooleanSelect Select widget with options 'Unknown', 'Yes' and 'No' ``SelectMultiple`` ~~~~~~~~~~~~~~~~~~ .. class:: SelectMultiple Similar to :class:`Select`, but allows multiple selection: ```` ``RadioSelect`` ~~~~~~~~~~~~~~~ .. class:: RadioSelect Similar to :class:`Select`, but rendered as a list of radio buttons within ``
  • `` tags: .. code-block:: html .. 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 %}
    {{ radio }}
    {% endfor %} This would generate the following HTML: .. code-block:: html
    That included the ``