======= 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. .. tip:: Widgets should not be confused with the :doc:`form fields `. Form fields deal with the logic of input validation and are used directly in templates. Widgets deal with rendering of HTML form input elements on the web page and extraction of raw submitted data. However, widgets do need to be :ref:`assigned ` to form fields. .. _widget-to-field: 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:`~django.forms.extras.widgets.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:: 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: .. _styling-widget-classes: Styling widget classes ^^^^^^^^^^^^^^^^^^^^^^ With widgets, it is possible to add media (``css`` and ``javascript``) and more deeply customize their appearance and behavior. In a nutshell, you will need to subclass the widget and either :ref:`define a class "Media" ` as a member of the subclass, or :ref:`create a property "media" `, returning an instance of that class. These methods involve somewhat advanced Python programming and are described in detail in the :doc:`Form Media ` topic guide. .. _base-widget-classes: Base Widget classes ------------------- Base widget classes :class:`Widget` and :class:`MultiWidget` are subclassed by all the :ref:`built-in widgets ` and may serve as a foundation for custom widgets. .. class:: Widget(attrs=None) This abstract class cannot be rendered, but provides the basic attribute :attr:`~Widget.attrs`. You may also implement or override the :meth:`~Widget.render()` method on custom widgets. .. 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'' .. method:: render(name, value, attrs=None) Returns HTML for the widget, as a Unicode string. This method must be implemented by the subclass, otherwise ``NotImplementedError`` will be raised. The 'value' given is not guaranteed to be valid input, therefore subclass implementations should program defensively. .. method:: value_from_datadict(self, data, files, name) Given a dictionary of data and this widget's name, returns the value of this widget. Returns ``None`` if a value wasn't provided. .. class:: MultiWidget(widgets, attrs=None) A widget that is composed of multiple widgets. :class:`~django.forms.MultiWidget` works hand in hand with the :class:`~django.forms.MultiValueField`. :class:`MultiWidget` has one required argument: .. attribute:: MultiWidget.widgets An iterable containing the widgets needed. And one required method: .. method:: decompress(value) This method takes a single "compressed" value from the field and returns a list of "decompressed" values. The input value can be assumed valid, but not necessarily non-empty. This method **must be implemented** by the subclass, and since the value may be empty, the implementation must be defensive. The rationale behind "decompression" is that it is necessary to "split" the combined value of the form field into the values for each widget. An example of this is how :class:`SplitDateTimeWidget` turns a :class:`~datetime.datetime` value into a list with date and time split into two separate values:: class SplitDateTimeWidget(MultiWidget): # ... def decompress(self, value): if value: return [value.date(), value.time().replace(microsecond=0)] return [None, None] .. tip:: Note that :class:`~django.forms.MultiValueField` has a complementary method :meth:`~django.forms.MultiValueField.compress` with the opposite responsibility - to combine cleaned values of all member fields into one. Other methods that may be useful to override include: .. method:: render(name, value, attrs=None) Argument ``value`` is handled differently in this method from the subclasses of :class:`~Widget` because it has to figure out how to split a single value for display in multiple widgets. The ``value`` argument used when rendering can be one of two things: * A ``list``. * A single value (e.g., a string) that is the "compressed" representation of a ``list`` of values. If `value` is a list, output of :meth:`~MultiWidget.render` will be a concatenation of rendered child widgets. If `value` is not a list, it will be first processed by the method :meth:`~MultiWidget.decompress()` to create the list and then processed as above. In the second case -- i.e., if the value is *not* a list -- ``render()`` will first decompress the value into a ``list`` before rendering it. It does so by calling the ``decompress()`` method, which :class:`MultiWidget`'s subclasses must implement (see above). When ``render()`` executes its HTML rendering, each value in the list is rendered with the corresponding widget -- the first value is rendered in the first widget, the second value is rendered in the second widget, etc. Unlike in the single value widgets, method :meth:`~MultiWidget.render` need not be implemented in the subclasses. .. method:: format_output(rendered_widgets) Given a list of rendered widgets (as strings), returns a Unicode string representing the HTML for the whole lot. This hook allows you to format the HTML design of the widgets any way you'd like. Here's an example widget which subclasses :class:`MultiWidget` to display a date with the day, month, and year in different select boxes. This widget is intended to be used with a :class:`~django.forms.DateField` rather than a :class:`~django.forms.MultiValueField`, thus we have implemented :meth:`~Widget.value_from_datadict`:: from datetime import date from django.forms import widgets class DateSelectorWidget(widgets.MultiWidget): def __init__(self, attrs=None): # create choices for days, months, years # example below, the rest snipped for brevity. years = [(year, year) for year in (2011, 2012, 2013)] _widgets = ( widgets.Select(attrs=attrs, choices=days), widgets.Select(attrs=attrs, choices=months), widgets.Select(attrs=attrs, choices=years), ) super(DateSelectorWidget, self).__init__(_widgets, attrs) def decompress(self, value): if value: return [value.day, value.month, value.year] return [None, None, None] def format_output(self, rendered_widgets): return u''.join(rendered_widgets) def value_from_datadict(self, data, files, name): datelist = [ widget.value_from_datadict(data, files, name + '_%s' % i) for i, widget in enumerate(self.widgets)] try: D = date(day=int(datelist[0]), month=int(datelist[1]), year=int(datelist[2])) except ValueError: return '' else: return str(D) The constructor creates several :class:`Select` widgets in a tuple. The ``super`` class uses this tuple to setup the widget. The :meth:`~MultiWidget.format_output` method is fairly vanilla here (in fact, it's the same as what's been implemented as the default for ``MultiWidget``), but the idea is that you could add custom HTML between the widgets should you wish. The required method :meth:`~MultiWidget.decompress` breaks up a ``datetime.date`` value into the day, month, and year values corresponding to each widget. Note how the method handles the case where ``value`` is ``None``. The default implementation of :meth:`~Widget.value_from_datadict` returns a list of values corresponding to each ``Widget``. This is appropriate when using a ``MultiWidget`` with a :class:`~django.forms.MultiValueField`, but since we want to use this widget with a :class:`~django.forms.DateField` which takes a single value, we have overridden this method to combine the data of all the subwidgets into a ``datetime.date``. The method extracts data from the ``POST`` dictionary and constructs and validates the date. If it is valid, we return the string, otherwise, we return an empty string which will cause ``form.is_valid`` to return ``False``. .. _built-in widgets: Built-in widgets ---------------- Django provides a representation of all the basic HTML widgets, plus some commonly used groups of widgets in the ``django.forms.widgets`` module, including :ref:`the input of text `, :ref:`various checkboxes and selectors `, :ref:`uploading files `, and :ref:`handling of multi-valued input `. .. _text-widgets: Widgets handling input of text ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ These widgets make use of the HTML elements ``input`` and ``textarea``. ``TextInput`` ~~~~~~~~~~~~~ .. class:: TextInput Text input: ```` ``EmailInput`` ~~~~~~~~~~~~~~ .. class:: EmailInput .. versionadded:: 1.6 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``). ``HiddenInput`` ~~~~~~~~~~~~~~~ .. class:: HiddenInput Hidden input: ```` Note that there also is a :class:`MultipleHiddenInput` widget that encapsulates a set of hidden input elements. ``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: ```` .. _selector-widgets: Selector and checkbox widgets ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``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 form field does not have a ``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
    • ...
    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 ``