2008-08-24 06:25:40 +08:00
|
|
|
=======
|
|
|
|
Widgets
|
|
|
|
=======
|
|
|
|
|
|
|
|
.. module:: django.forms.widgets
|
|
|
|
:synopsis: Django's built-in form widgets.
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. 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.
|
|
|
|
|
|
|
|
Django provides a representation of all the basic HTML widgets, plus some
|
|
|
|
commonly used groups of widgets:
|
|
|
|
|
|
|
|
.. class:: TextInput
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Text input: ``<input type='text' ...>``
|
|
|
|
|
|
|
|
.. class:: PasswordInput
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Password input: ``<input type='password' ...>``
|
|
|
|
|
2009-08-29 20:40:47 +08:00
|
|
|
Takes one optional argument:
|
|
|
|
|
|
|
|
.. attribute:: PasswordInput.render_value
|
|
|
|
|
|
|
|
Determines whether the widget will have a value filled in when the
|
2010-08-06 22:25:58 +08:00
|
|
|
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``
|
2009-08-29 20:40:47 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: HiddenInput
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Hidden input: ``<input type='hidden' ...>``
|
|
|
|
|
|
|
|
.. class:: MultipleHiddenInput
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Multiple ``<input type='hidden' ...>`` widgets.
|
|
|
|
|
|
|
|
.. class:: FileInput
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
File upload input: ``<input type='file' ...>``
|
|
|
|
|
2009-03-23 00:13:06 +08:00
|
|
|
.. class:: DateInput
|
|
|
|
|
|
|
|
.. versionadded:: 1.1
|
|
|
|
|
|
|
|
Date input as a simple text box: ``<input type='text' ...>``
|
|
|
|
|
|
|
|
Takes one optional argument:
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2009-03-23 00:13:06 +08:00
|
|
|
.. 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 ``'%Y-%m-%d'``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: DateTimeInput
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
.. versionadded:: 1.0
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
Date/time input as a simple text box: ``<input type='text' ...>``
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2009-03-23 00:13:06 +08:00
|
|
|
Takes one optional argument:
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2009-03-23 00:13:06 +08:00
|
|
|
.. attribute:: DateTimeInput.format
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2009-03-23 00:13:06 +08:00
|
|
|
The format in which this field's initial value will be displayed.
|
2010-08-06 22:25:58 +08:00
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
If no ``format`` argument is provided, the default format is ``'%Y-%m-%d
|
|
|
|
%H:%M:%S'``.
|
2009-03-23 00:13:06 +08:00
|
|
|
|
|
|
|
.. class:: TimeInput
|
|
|
|
|
|
|
|
Time input as a simple text box: ``<input type='text' ...>``
|
|
|
|
|
|
|
|
Takes one optional argument:
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2009-03-23 00:13:06 +08:00
|
|
|
.. attribute:: TimeInput.format
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2009-03-23 00:13:06 +08:00
|
|
|
The format in which this field's initial value will be displayed.
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2009-03-23 00:13:06 +08:00
|
|
|
If no ``format`` argument is provided, the default format is ``'%H:%M:%S'``.
|
|
|
|
|
|
|
|
.. versionchanged:: 1.1
|
|
|
|
The ``format`` argument was not supported in Django 1.0.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: Textarea
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Text area: ``<textarea>...</textarea>``
|
|
|
|
|
|
|
|
.. class:: CheckboxInput
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Checkbox: ``<input type='checkbox' ...>``
|
|
|
|
|
2009-08-29 20:40:47 +08:00
|
|
|
Takes one optional argument:
|
|
|
|
|
|
|
|
.. attribute:: CheckboxInput.check_test
|
2010-08-06 22:25:58 +08:00
|
|
|
|
|
|
|
A callable that takes the value of the CheckBoxInput
|
2009-08-29 20:40:47 +08:00
|
|
|
and returns ``True`` if the checkbox should be checked for
|
2010-08-06 22:25:58 +08:00
|
|
|
that value.
|
2009-08-29 20:40:47 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: Select
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Select widget: ``<select><option ...>...</select>``
|
2010-08-06 22:25:58 +08:00
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
Requires that your field provides :attr:`~Field.choices`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. class:: NullBooleanSelect
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Select widget with options 'Unknown', 'Yes' and 'No'
|
|
|
|
|
|
|
|
.. class:: SelectMultiple
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Select widget allowing multiple selection: ``<select
|
|
|
|
multiple='multiple'>...</select>``
|
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
Requires that your field provides :attr:`~Field.choices`.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: RadioSelect
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
A list of radio buttons:
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. code-block:: html
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
<ul>
|
|
|
|
<li><input type='radio' ...></li>
|
|
|
|
...
|
|
|
|
</ul>
|
2010-08-06 22:25:58 +08:00
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
Requires that your field provides :attr:`~Field.choices`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. class:: CheckboxSelectMultiple
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
A list of checkboxes:
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. code-block:: html
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
<ul>
|
|
|
|
<li><input type='checkbox' ...></li>
|
|
|
|
...
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
.. class:: MultiWidget
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Wrapper around multiple other widgets
|
|
|
|
|
|
|
|
.. class:: SplitDateTimeWidget
|
2008-08-27 13:56:57 +08:00
|
|
|
|
2009-03-23 00:13:06 +08:00
|
|
|
Wrapper around two widgets: ``DateInput`` for the date, and ``TimeInput``
|
|
|
|
for the time.
|
|
|
|
|
|
|
|
Takes two optional arguments, ``date_format`` and ``time_format``, which
|
|
|
|
work just like the ``format`` argument for ``DateInput`` and ``TimeInput``.
|
2010-08-06 22:25:58 +08:00
|
|
|
|
2009-03-23 00:13:06 +08:00
|
|
|
.. versionchanged:: 1.1
|
|
|
|
The ``date_format`` and ``time_format`` arguments were not supported in Django 1.0.
|
|
|
|
|
2009-09-10 23:45:15 +08:00
|
|
|
.. class:: SelectDateWidget
|
|
|
|
|
|
|
|
Wrapper around three select widgets: one each for month, day, and year.
|
|
|
|
Note that this widget lives in a separate file from the standard widgets.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from django.forms.extras.widgets import SelectDateWidget
|
|
|
|
|
|
|
|
date = forms.DateField(widget=SelectDateWidget())
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Specifying widgets
|
|
|
|
------------------
|
|
|
|
|
|
|
|
.. attribute:: Form.widget
|
|
|
|
|
|
|
|
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 for the
|
|
|
|
built-in Field classes.
|
|
|
|
|
|
|
|
However, if you want to use a different widget for a field, you can -
|
|
|
|
just use the 'widget' argument on the field definition. For example::
|
|
|
|
|
2009-02-21 16:45:47 +08:00
|
|
|
from django import forms
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
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 Textarea widget,
|
|
|
|
rather than the default TextInput widget.
|
|
|
|
|
|
|
|
Customizing widget instances
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
When Django renders a widget as HTML, it only renders the bare minimum
|
|
|
|
HTML - Django doesn't add a class definition, or any other widget-specific
|
|
|
|
attributes. This means that all 'TextInput' widgets will appear the same
|
|
|
|
on your web page.
|
|
|
|
|
|
|
|
If you want to make one widget look different to another, you need to
|
|
|
|
specify additional attributes for each widget. When you specify a
|
|
|
|
widget, you can provide a list of attributes that will be added to the
|
|
|
|
rendered HTML for the widget.
|
|
|
|
|
|
|
|
For example, take the following simple form::
|
|
|
|
|
|
|
|
class CommentForm(forms.Form):
|
|
|
|
name = forms.CharField()
|
|
|
|
url = forms.URLField()
|
|
|
|
comment = forms.CharField()
|
|
|
|
|
|
|
|
This form will include three default TextInput widgets, with default rendering -
|
|
|
|
no CSS class, no extra attributes. This means that the input boxes provided for
|
|
|
|
each widget will be rendered exactly the same::
|
|
|
|
|
|
|
|
>>> f = CommentForm(auto_id=False)
|
|
|
|
>>> f.as_table()
|
|
|
|
<tr><th>Name:</th><td><input type="text" name="name" /></td></tr>
|
|
|
|
<tr><th>Url:</th><td><input type="text" name="url"/></td></tr>
|
|
|
|
<tr><th>Comment:</th><td><input type="text" name="comment" /></td></tr>
|
|
|
|
|
|
|
|
|
|
|
|
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. To do this, you use the ``attrs``
|
|
|
|
argument when creating the widget:
|
|
|
|
|
|
|
|
.. attribute:: Widget.attrs
|
|
|
|
|
|
|
|
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()
|
|
|
|
<tr><th>Name:</th><td><input type="text" name="name" class="special"/></td></tr>
|
|
|
|
<tr><th>Url:</th><td><input type="text" name="url"/></td></tr>
|
|
|
|
<tr><th>Comment:</th><td><input type="text" name="comment" size="40"/></td></tr>
|