File: widgets.txt

package info (click to toggle)
python-django 1.0.2-1%2Blenny3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 17,652 kB
  • ctags: 7,831
  • sloc: python: 46,969; makefile: 78; xml: 34; sql: 33; sh: 16
file content (171 lines) | stat: -rw-r--r-- 4,822 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
.. _ref-forms-widgets:

=======
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.

Django provides a representation of all the basic HTML widgets, plus some
commonly used groups of widgets:

.. class:: TextInput

    Text input: ``<input type='text' ...>``

.. class:: PasswordInput

    Password input: ``<input type='password' ...>``

.. class:: HiddenInput

    Hidden input: ``<input type='hidden' ...>``

.. class:: MultipleHiddenInput

    Multiple ``<input type='hidden' ...>`` widgets.

.. class:: FileInput

    File upload input: ``<input type='file' ...>``

.. class:: DateTimeInput

    .. versionadded:: 1.0

    Date/time input as a simple text box: ``<input type='text' ...>``

.. class:: Textarea

    Text area: ``<textarea>...</textarea>``

.. class:: CheckboxInput

    Checkbox: ``<input type='checkbox' ...>``

.. class:: Select

    Select widget: ``<select><option ...>...</select>``

.. class:: NullBooleanSelect

    Select widget with options 'Unknown', 'Yes' and 'No'

.. class:: SelectMultiple

    Select widget allowing multiple selection: ``<select
    multiple='multiple'>...</select>``

.. class:: RadioSelect

    A list of radio buttons:
    
    .. code-block:: html
    
        <ul>
          <li><input type='radio' ...></li>
          ...
        </ul>

.. class:: CheckboxSelectMultiple

    A list of checkboxes:
    
    .. code-block:: html
    
        <ul>
          <li><input type='checkbox' ...></li>
          ...
        </ul>

.. class:: MultiWidget

    Wrapper around multiple other widgets

.. class:: SplitDateTimeWidget

    Wrapper around two ``TextInput`` widgets: one for the date, and one for the
    time.

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::

    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>