diff options
| author | Andrew Godwin <andrew@aeracode.org> | 2013-04-18 17:16:39 +0100 |
|---|---|---|
| committer | Andrew Godwin <andrew@aeracode.org> | 2013-04-18 17:16:39 +0100 |
| commit | 7f3678dc4cd7146c49bac3fb8f5211f647636aa3 (patch) | |
| tree | fdd2a60ccd1a9a3162d89f970db44502e35a3b3f /docs/ref/forms | |
| parent | b62e82365ad56ca930f7abb1d1dbdf9ce5a7c7c3 (diff) | |
| parent | 93c1576f17f6c5ee73f94f8de007f3c33010dc81 (diff) | |
Merge branch 'master' into schema-alteration
Conflicts:
django/db/backends/__init__.py
django/db/backends/mysql/base.py
django/db/backends/oracle/base.py
django/db/backends/oracle/creation.py
django/db/backends/postgresql_psycopg2/base.py
django/db/backends/sqlite3/base.py
django/db/models/fields/related.py
Diffstat (limited to 'docs/ref/forms')
| -rw-r--r-- | docs/ref/forms/api.txt | 119 | ||||
| -rw-r--r-- | docs/ref/forms/fields.txt | 85 | ||||
| -rw-r--r-- | docs/ref/forms/formsets.txt | 16 | ||||
| -rw-r--r-- | docs/ref/forms/index.txt | 2 | ||||
| -rw-r--r-- | docs/ref/forms/models.txt | 60 | ||||
| -rw-r--r-- | docs/ref/forms/validation.txt | 20 | ||||
| -rw-r--r-- | docs/ref/forms/widgets.txt | 87 |
7 files changed, 259 insertions, 130 deletions
diff --git a/docs/ref/forms/api.txt b/docs/ref/forms/api.txt index dffef314b7..34ed2e493e 100644 --- a/docs/ref/forms/api.txt +++ b/docs/ref/forms/api.txt @@ -2,9 +2,7 @@ The Forms API ============= -.. module:: django.forms.forms - -.. currentmodule:: django.forms +.. module:: django.forms .. admonition:: About this document @@ -150,11 +148,11 @@ it's not necessary to include every field in your form. For example:: These values are only displayed for unbound forms, and they're not used as fallback values if a particular value isn't provided. -Note that if a :class:`~django.forms.fields.Field` defines -:attr:`~Form.initial` *and* you include ``initial`` when instantiating the -``Form``, then the latter ``initial`` will have precedence. In this example, -``initial`` is provided both at the field level and at the form instance level, -and the latter gets precedence:: +Note that if a :class:`~django.forms.Field` defines :attr:`~Form.initial` *and* +you include ``initial`` when instantiating the ``Form``, then the latter +``initial`` will have precedence. In this example, ``initial`` is provided both +at the field level and at the form instance level, and the latter gets +precedence:: >>> class CommentForm(forms.Form): ... name = forms.CharField(initial='class') @@ -163,7 +161,7 @@ and the latter gets precedence:: >>> f = CommentForm(initial={'name': 'instance'}, auto_id=False) >>> print(f) <tr><th>Name:</th><td><input type="text" name="name" value="instance" /></td></tr> - <tr><th>Url:</th><td><input type="text" name="url" /></td></tr> + <tr><th>Url:</th><td><input type="url" name="url" /></td></tr> <tr><th>Comment:</th><td><input type="text" name="comment" /></td></tr> Accessing "clean" data @@ -272,7 +270,7 @@ simply ``print`` it:: >>> print(f) <tr><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr> <tr><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr> - <tr><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr> + <tr><th><label for="id_sender">Sender:</label></th><td><input type="email" name="sender" id="id_sender" /></td></tr> <tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr> If the form is bound to data, the HTML output will include that data @@ -289,7 +287,7 @@ include ``checked="checked"`` if appropriate:: >>> print(f) <tr><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" value="hello" /></td></tr> <tr><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" value="Hi there" /></td></tr> - <tr><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" value="foo@example.com" /></td></tr> + <tr><th><label for="id_sender">Sender:</label></th><td><input type="email" name="sender" id="id_sender" value="foo@example.com" /></td></tr> <tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" checked="checked" /></td></tr> This default output is a two-column HTML table, with a ``<tr>`` for each field. @@ -299,8 +297,9 @@ Notice the following: ``</table>`` tags, nor does it include the ``<form>`` and ``</form>`` tags or an ``<input type="submit">`` tag. It's your job to do that. -* Each field type has a default HTML representation. ``CharField`` and - ``EmailField`` are represented by an ``<input type="text">``. +* Each field type has a default HTML representation. ``CharField`` is + represented by an ``<input type="text">`` and ``EmailField`` by an + ``<input type="email">``. ``BooleanField`` is represented by an ``<input type="checkbox">``. Note these are merely sensible defaults; you can specify which HTML to use for a given field by using widgets, which we'll explain shortly. @@ -337,7 +336,7 @@ a form object, and each rendering method returns a Unicode object. >>> print(f.as_p()) <p><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></p> <p><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></p> - <p><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></p> + <p><label for="id_sender">Sender:</label> <input type="email" name="sender" id="id_sender" /></p> <p><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p> ``as_ul()`` @@ -352,11 +351,11 @@ a form object, and each rendering method returns a Unicode object. >>> f = ContactForm() >>> f.as_ul() - u'<li><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></li>\n<li><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></li>\n<li><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li>\n<li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li>' + u'<li><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></li>\n<li><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></li>\n<li><label for="id_sender">Sender:</label> <input type="email" name="sender" id="id_sender" /></li>\n<li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li>' >>> print(f.as_ul()) <li><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></li> <li><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></li> - <li><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li> + <li><label for="id_sender">Sender:</label> <input type="email" name="sender" id="id_sender" /></li> <li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li> ``as_table()`` @@ -370,16 +369,19 @@ a form object, and each rendering method returns a Unicode object. >>> f = ContactForm() >>> f.as_table() - u'<tr><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr>\n<tr><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr>\n<tr><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr>\n<tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr>' + u'<tr><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr>\n<tr><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr>\n<tr><th><label for="id_sender">Sender:</label></th><td><input type="email" name="sender" id="id_sender" /></td></tr>\n<tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr>' >>> print(f.as_table()) <tr><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr> <tr><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr> - <tr><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr> + <tr><th><label for="id_sender">Sender:</label></th><td><input type="email" name="sender" id="id_sender" /></td></tr> <tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr> Styling required or erroneous form rows ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +.. attribute:: Form.error_css_class +.. attribute:: Form.required_css_class + It's pretty common to style form rows and fields that are required or have errors. For example, you might want to present required form rows in bold and highlight errors in red. @@ -430,17 +432,17 @@ tags nor ``id`` attributes:: >>> print(f.as_table()) <tr><th>Subject:</th><td><input type="text" name="subject" maxlength="100" /></td></tr> <tr><th>Message:</th><td><input type="text" name="message" /></td></tr> - <tr><th>Sender:</th><td><input type="text" name="sender" /></td></tr> + <tr><th>Sender:</th><td><input type="email" name="sender" /></td></tr> <tr><th>Cc myself:</th><td><input type="checkbox" name="cc_myself" /></td></tr> >>> print(f.as_ul()) <li>Subject: <input type="text" name="subject" maxlength="100" /></li> <li>Message: <input type="text" name="message" /></li> - <li>Sender: <input type="text" name="sender" /></li> + <li>Sender: <input type="email" name="sender" /></li> <li>Cc myself: <input type="checkbox" name="cc_myself" /></li> >>> print(f.as_p()) <p>Subject: <input type="text" name="subject" maxlength="100" /></p> <p>Message: <input type="text" name="message" /></p> - <p>Sender: <input type="text" name="sender" /></p> + <p>Sender: <input type="email" name="sender" /></p> <p>Cc myself: <input type="checkbox" name="cc_myself" /></p> If ``auto_id`` is set to ``True``, then the form output *will* include @@ -451,17 +453,17 @@ field:: >>> print(f.as_table()) <tr><th><label for="subject">Subject:</label></th><td><input id="subject" type="text" name="subject" maxlength="100" /></td></tr> <tr><th><label for="message">Message:</label></th><td><input type="text" name="message" id="message" /></td></tr> - <tr><th><label for="sender">Sender:</label></th><td><input type="text" name="sender" id="sender" /></td></tr> + <tr><th><label for="sender">Sender:</label></th><td><input type="email" name="sender" id="sender" /></td></tr> <tr><th><label for="cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="cc_myself" /></td></tr> >>> print(f.as_ul()) <li><label for="subject">Subject:</label> <input id="subject" type="text" name="subject" maxlength="100" /></li> <li><label for="message">Message:</label> <input type="text" name="message" id="message" /></li> - <li><label for="sender">Sender:</label> <input type="text" name="sender" id="sender" /></li> + <li><label for="sender">Sender:</label> <input type="email" name="sender" id="sender" /></li> <li><label for="cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="cc_myself" /></li> >>> print(f.as_p()) <p><label for="subject">Subject:</label> <input id="subject" type="text" name="subject" maxlength="100" /></p> <p><label for="message">Message:</label> <input type="text" name="message" id="message" /></p> - <p><label for="sender">Sender:</label> <input type="text" name="sender" id="sender" /></p> + <p><label for="sender">Sender:</label> <input type="email" name="sender" id="sender" /></p> <p><label for="cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="cc_myself" /></p> If ``auto_id`` is set to a string containing the format character ``'%s'``, @@ -474,17 +476,17 @@ attributes based on the format string. For example, for a format string >>> print(f.as_table()) <tr><th><label for="id_for_subject">Subject:</label></th><td><input id="id_for_subject" type="text" name="subject" maxlength="100" /></td></tr> <tr><th><label for="id_for_message">Message:</label></th><td><input type="text" name="message" id="id_for_message" /></td></tr> - <tr><th><label for="id_for_sender">Sender:</label></th><td><input type="text" name="sender" id="id_for_sender" /></td></tr> + <tr><th><label for="id_for_sender">Sender:</label></th><td><input type="email" name="sender" id="id_for_sender" /></td></tr> <tr><th><label for="id_for_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></td></tr> >>> print(f.as_ul()) <li><label for="id_for_subject">Subject:</label> <input id="id_for_subject" type="text" name="subject" maxlength="100" /></li> <li><label for="id_for_message">Message:</label> <input type="text" name="message" id="id_for_message" /></li> - <li><label for="id_for_sender">Sender:</label> <input type="text" name="sender" id="id_for_sender" /></li> + <li><label for="id_for_sender">Sender:</label> <input type="email" name="sender" id="id_for_sender" /></li> <li><label for="id_for_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></li> >>> print(f.as_p()) <p><label for="id_for_subject">Subject:</label> <input id="id_for_subject" type="text" name="subject" maxlength="100" /></p> <p><label for="id_for_message">Message:</label> <input type="text" name="message" id="id_for_message" /></p> - <p><label for="id_for_sender">Sender:</label> <input type="text" name="sender" id="id_for_sender" /></p> + <p><label for="id_for_sender">Sender:</label> <input type="email" name="sender" id="id_for_sender" /></p> <p><label for="id_for_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></p> If ``auto_id`` is set to any other true value -- such as a string that doesn't @@ -500,13 +502,13 @@ entirely, using the ``label_suffix`` parameter:: >>> print(f.as_ul()) <li><label for="id_for_subject">Subject</label> <input id="id_for_subject" type="text" name="subject" maxlength="100" /></li> <li><label for="id_for_message">Message</label> <input type="text" name="message" id="id_for_message" /></li> - <li><label for="id_for_sender">Sender</label> <input type="text" name="sender" id="id_for_sender" /></li> + <li><label for="id_for_sender">Sender</label> <input type="email" name="sender" id="id_for_sender" /></li> <li><label for="id_for_cc_myself">Cc myself</label> <input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></li> >>> f = ContactForm(auto_id='id_for_%s', label_suffix=' ->') >>> print(f.as_ul()) <li><label for="id_for_subject">Subject -></label> <input id="id_for_subject" type="text" name="subject" maxlength="100" /></li> <li><label for="id_for_message">Message -></label> <input type="text" name="message" id="id_for_message" /></li> - <li><label for="id_for_sender">Sender -></label> <input type="text" name="sender" id="id_for_sender" /></li> + <li><label for="id_for_sender">Sender -></label> <input type="email" name="sender" id="id_for_sender" /></li> <li><label for="id_for_cc_myself">Cc myself -></label> <input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></li> Note that the label suffix is added only if the last character of the @@ -538,19 +540,19 @@ method you're using:: >>> print(f.as_table()) <tr><th>Subject:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="subject" maxlength="100" /></td></tr> <tr><th>Message:</th><td><input type="text" name="message" value="Hi there" /></td></tr> - <tr><th>Sender:</th><td><ul class="errorlist"><li>Enter a valid email address.</li></ul><input type="text" name="sender" value="invalid email address" /></td></tr> + <tr><th>Sender:</th><td><ul class="errorlist"><li>Enter a valid email address.</li></ul><input type="email" name="sender" value="invalid email address" /></td></tr> <tr><th>Cc myself:</th><td><input checked="checked" type="checkbox" name="cc_myself" /></td></tr> >>> print(f.as_ul()) <li><ul class="errorlist"><li>This field is required.</li></ul>Subject: <input type="text" name="subject" maxlength="100" /></li> <li>Message: <input type="text" name="message" value="Hi there" /></li> - <li><ul class="errorlist"><li>Enter a valid email address.</li></ul>Sender: <input type="text" name="sender" value="invalid email address" /></li> + <li><ul class="errorlist"><li>Enter a valid email address.</li></ul>Sender: <input type="email" name="sender" value="invalid email address" /></li> <li>Cc myself: <input checked="checked" type="checkbox" name="cc_myself" /></li> >>> print(f.as_p()) <p><ul class="errorlist"><li>This field is required.</li></ul></p> <p>Subject: <input type="text" name="subject" maxlength="100" /></p> <p>Message: <input type="text" name="message" value="Hi there" /></p> <p><ul class="errorlist"><li>Enter a valid email address.</li></ul></p> - <p>Sender: <input type="text" name="sender" value="invalid email address" /></p> + <p>Sender: <input type="email" name="sender" value="invalid email address" /></p> <p>Cc myself: <input checked="checked" type="checkbox" name="cc_myself" /></p> Customizing the error list format @@ -573,7 +575,7 @@ pass that in at construction time:: <p>Subject: <input type="text" name="subject" maxlength="100" /></p> <p>Message: <input type="text" name="message" value="Hi there" /></p> <div class="errorlist"><div class="error">Enter a valid email address.</div></div> - <p>Sender: <input type="text" name="sender" value="invalid email address" /></p> + <p>Sender: <input type="email" name="sender" value="invalid email address" /></p> <p>Cc myself: <input checked="checked" type="checkbox" name="cc_myself" /></p> More granular output @@ -587,24 +589,24 @@ lazy developers -- they're not the only way a form object can be displayed. Used to display HTML or access attributes for a single field of a :class:`Form` instance. - The :meth:`__unicode__` and :meth:`__str__` methods of this object displays + The ``__unicode__()`` and ``__str__()`` methods of this object displays the HTML for this field. To retrieve a single ``BoundField``, use dictionary lookup syntax on your form using the field's name as the key:: - >>> form = ContactForm() - >>> print(form['subject']) - <input id="id_subject" type="text" name="subject" maxlength="100" /> + >>> form = ContactForm() + >>> print(form['subject']) + <input id="id_subject" type="text" name="subject" maxlength="100" /> To retrieve all ``BoundField`` objects, iterate the form:: - >>> form = ContactForm() - >>> for boundfield in form: print(boundfield) - <input id="id_subject" type="text" name="subject" maxlength="100" /> - <input type="text" name="message" id="id_message" /> - <input type="text" name="sender" id="id_sender" /> - <input type="checkbox" name="cc_myself" id="id_cc_myself" /> + >>> form = ContactForm() + >>> for boundfield in form: print(boundfield) + <input id="id_subject" type="text" name="subject" maxlength="100" /> + <input type="text" name="message" id="id_message" /> + <input type="email" name="sender" id="id_sender" /> + <input type="checkbox" name="cc_myself" id="id_cc_myself" /> The field-specific output honors the form object's ``auto_id`` setting:: @@ -635,7 +637,20 @@ For a field's list of errors, access the field's ``errors`` attribute. >>> print(f['subject'].errors) >>> str(f['subject'].errors) - '' + '' + +.. method:: BoundField.label_tag(contents=None, attrs=None) + +To separately render the label tag of a form field, you can call its +``label_tag`` method:: + + >>> f = ContactForm(data) + >>> print(f['message'].label_tag()) + <label for="id_message">Message</label> + +Optionally, you can provide the ``contents`` parameter which will replace the +auto-generated label tag. An optional ``attrs`` dictionary may contain +additional attributes for the ``<label>`` tag. .. method:: BoundField.css_classes() @@ -644,17 +659,17 @@ indicate required form fields or fields that contain errors. If you're manually rendering a form, you can access these CSS classes using the ``css_classes`` method:: - >>> f = ContactForm(data) - >>> f['message'].css_classes() - 'required' + >>> f = ContactForm(data) + >>> f['message'].css_classes() + 'required' If you want to provide some additional classes in addition to the error and required classes that may be required, you can provide those classes as an argument:: - >>> f = ContactForm(data) - >>> f['message'].css_classes('foo bar') - 'foo bar required' + >>> f = ContactForm(data) + >>> f['message'].css_classes('foo bar') + 'foo bar required' .. method:: BoundField.value() @@ -715,6 +730,8 @@ form data *and* file data:: Testing for multipart forms ~~~~~~~~~~~~~~~~~~~~~~~~~~~ +.. method:: Form.is_multipart + If you're writing reusable views or templates, you may not know ahead of time whether your form is a multipart form or not. The ``is_multipart()`` method tells you whether the form requires multipart encoding for submission:: @@ -753,7 +770,7 @@ fields are ordered first:: >>> print(f.as_ul()) <li>Subject: <input type="text" name="subject" maxlength="100" /></li> <li>Message: <input type="text" name="message" /></li> - <li>Sender: <input type="text" name="sender" /></li> + <li>Sender: <input type="email" name="sender" /></li> <li>Cc myself: <input type="checkbox" name="cc_myself" /></li> <li>Priority: <input type="text" name="priority" /></li> diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt index 75d05c6829..c8b8044d26 100644 --- a/docs/ref/forms/fields.txt +++ b/docs/ref/forms/fields.txt @@ -112,7 +112,7 @@ We've specified ``auto_id=False`` to simplify the output:: >>> f = CommentForm(auto_id=False) >>> print(f) <tr><th>Your name:</th><td><input type="text" name="name" /></td></tr> - <tr><th>Your Web site:</th><td><input type="text" name="url" /></td></tr> + <tr><th>Your Web site:</th><td><input type="url" name="url" /></td></tr> <tr><th>Comment:</th><td><input type="text" name="comment" /></td></tr> ``initial`` @@ -135,7 +135,7 @@ field is initialized to a particular value. For example:: >>> f = CommentForm(auto_id=False) >>> print(f) <tr><th>Name:</th><td><input type="text" name="name" value="Your name" /></td></tr> - <tr><th>Url:</th><td><input type="text" name="url" value="http://" /></td></tr> + <tr><th>Url:</th><td><input type="url" name="url" value="http://" /></td></tr> <tr><th>Comment:</th><td><input type="text" name="comment" /></td></tr> You may be thinking, why not just pass a dictionary of the initial values as @@ -150,7 +150,7 @@ and the HTML output will include any validation errors:: >>> f = CommentForm(default_data, auto_id=False) >>> print(f) <tr><th>Name:</th><td><input type="text" name="name" value="Your name" /></td></tr> - <tr><th>Url:</th><td><ul class="errorlist"><li>Enter a valid URL.</li></ul><input type="text" name="url" value="http://" /></td></tr> + <tr><th>Url:</th><td><ul class="errorlist"><li>Enter a valid URL.</li></ul><input type="url" name="url" value="http://" /></td></tr> <tr><th>Comment:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="comment" /></td></tr> This is why ``initial`` values are only displayed for unbound forms. For bound @@ -212,17 +212,17 @@ fields. We've specified ``auto_id=False`` to simplify the output:: >>> print(f.as_table()) <tr><th>Subject:</th><td><input type="text" name="subject" maxlength="100" /><br /><span class="helptext">100 characters max.</span></td></tr> <tr><th>Message:</th><td><input type="text" name="message" /></td></tr> - <tr><th>Sender:</th><td><input type="text" name="sender" /><br />A valid email address, please.</td></tr> + <tr><th>Sender:</th><td><input type="email" name="sender" /><br />A valid email address, please.</td></tr> <tr><th>Cc myself:</th><td><input type="checkbox" name="cc_myself" /></td></tr> >>> print(f.as_ul())) <li>Subject: <input type="text" name="subject" maxlength="100" /> <span class="helptext">100 characters max.</span></li> <li>Message: <input type="text" name="message" /></li> - <li>Sender: <input type="text" name="sender" /> A valid email address, please.</li> + <li>Sender: <input type="email" name="sender" /> A valid email address, please.</li> <li>Cc myself: <input type="checkbox" name="cc_myself" /></li> >>> print(f.as_p()) <p>Subject: <input type="text" name="subject" maxlength="100" /> <span class="helptext">100 characters max.</span></p> <p>Message: <input type="text" name="message" /></p> - <p>Sender: <input type="text" name="sender" /> A valid email address, please.</p> + <p>Sender: <input type="email" name="sender" /> A valid email address, please.</p> <p>Cc myself: <input type="checkbox" name="cc_myself" /></p> ``error_messages`` @@ -289,7 +289,7 @@ For each field, we describe the default widget used if you don't specify .. class:: BooleanField(**kwargs) - * Default widget: ``CheckboxInput`` + * Default widget: :class:`CheckboxInput` * Empty value: ``False`` * Normalizes to: A Python ``True`` or ``False`` value. * Validates that the value is ``True`` (e.g. the check box is checked) if @@ -309,7 +309,7 @@ For each field, we describe the default widget used if you don't specify .. class:: CharField(**kwargs) - * Default widget: ``TextInput`` + * Default widget: :class:`TextInput` * Empty value: ``''`` (an empty string) * Normalizes to: A Unicode object. * Validates ``max_length`` or ``min_length``, if they are provided. @@ -329,7 +329,7 @@ For each field, we describe the default widget used if you don't specify .. class:: ChoiceField(**kwargs) - * Default widget: ``Select`` + * Default widget: :class:`Select` * Empty value: ``''`` (an empty string) * Normalizes to: A Unicode object. * Validates that the given value exists in the list of choices. @@ -355,7 +355,7 @@ For each field, we describe the default widget used if you don't specify Just like a :class:`ChoiceField`, except :class:`TypedChoiceField` takes two extra arguments, ``coerce`` and ``empty_value``. - * Default widget: ``Select`` + * Default widget: :class:`Select` * Empty value: Whatever you've given as ``empty_value`` * Normalizes to: A value of the type provided by the ``coerce`` argument. * Validates that the given value exists in the list of choices and can be @@ -382,7 +382,7 @@ For each field, we describe the default widget used if you don't specify .. class:: DateField(**kwargs) - * Default widget: ``DateInput`` + * Default widget: :class:`DateInput` * Empty value: ``None`` * Normalizes to: A Python ``datetime.date`` object. * Validates that the given value is either a ``datetime.date``, @@ -398,21 +398,21 @@ For each field, we describe the default widget used if you don't specify If no ``input_formats`` argument is provided, the default input formats are:: - '%Y-%m-%d', # '2006-10-25' + ['%Y-%m-%d', # '2006-10-25' '%m/%d/%Y', # '10/25/2006' - '%m/%d/%y', # '10/25/06' + '%m/%d/%y'] # '10/25/06' Additionally, if you specify :setting:`USE_L10N=False<USE_L10N>` in your settings, the following will also be included in the default input formats:: - '%b %d %Y', # 'Oct 25 2006' + ['%b %d %Y', # 'Oct 25 2006' '%b %d, %Y', # 'Oct 25, 2006' '%d %b %Y', # '25 Oct 2006' '%d %b, %Y', # '25 Oct, 2006' '%B %d %Y', # 'October 25 2006' '%B %d, %Y', # 'October 25, 2006' '%d %B %Y', # '25 October 2006' - '%d %B, %Y', # '25 October, 2006' + '%d %B, %Y'] # '25 October, 2006' See also :ref:`format localization <format-localization>`. @@ -421,7 +421,7 @@ For each field, we describe the default widget used if you don't specify .. class:: DateTimeField(**kwargs) - * Default widget: ``DateTimeInput`` + * Default widget: :class:`DateTimeInput` * Empty value: ``None`` * Normalizes to: A Python ``datetime.datetime`` object. * Validates that the given value is either a ``datetime.datetime``, @@ -437,7 +437,7 @@ For each field, we describe the default widget used if you don't specify If no ``input_formats`` argument is provided, the default input formats are:: - '%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59' + ['%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59' '%Y-%m-%d %H:%M', # '2006-10-25 14:30' '%Y-%m-%d', # '2006-10-25' '%m/%d/%Y %H:%M:%S', # '10/25/2006 14:30:59' @@ -445,7 +445,7 @@ For each field, we describe the default widget used if you don't specify '%m/%d/%Y', # '10/25/2006' '%m/%d/%y %H:%M:%S', # '10/25/06 14:30:59' '%m/%d/%y %H:%M', # '10/25/06 14:30' - '%m/%d/%y', # '10/25/06' + '%m/%d/%y'] # '10/25/06' See also :ref:`format localization <format-localization>`. @@ -454,7 +454,8 @@ For each field, we describe the default widget used if you don't specify .. class:: DecimalField(**kwargs) - * Default widget: ``TextInput`` + * Default widget: :class:`NumberInput` when :attr:`Field.localize` is + ``False``, else :class:`TextInput`. * Empty value: ``None`` * Normalizes to: A Python ``decimal``. * Validates that the given value is a decimal. Leading and trailing @@ -489,7 +490,7 @@ For each field, we describe the default widget used if you don't specify .. class:: EmailField(**kwargs) - * Default widget: ``TextInput`` + * Default widget: :class:`EmailInput` * Empty value: ``''`` (an empty string) * Normalizes to: A Unicode object. * Validates that the given value is a valid email address, using a @@ -505,7 +506,7 @@ For each field, we describe the default widget used if you don't specify .. class:: FileField(**kwargs) - * Default widget: ``ClearableFileInput`` + * Default widget: :class:`ClearableFileInput` * Empty value: ``None`` * Normalizes to: An ``UploadedFile`` object that wraps the file content and file name into a single object. @@ -533,7 +534,7 @@ For each field, we describe the default widget used if you don't specify .. class:: FilePathField(**kwargs) - * Default widget: ``Select`` + * Default widget: :class:`Select` * Empty value: ``None`` * Normalizes to: A unicode object * Validates that the selected choice exists in the list of choices. @@ -580,7 +581,8 @@ For each field, we describe the default widget used if you don't specify .. class:: FloatField(**kwargs) - * Default widget: ``TextInput`` + * Default widget: :class:`NumberInput` when :attr:`Field.localize` is + ``False``, else :class:`TextInput`. * Empty value: ``None`` * Normalizes to: A Python float. * Validates that the given value is an float. Leading and trailing @@ -596,7 +598,7 @@ For each field, we describe the default widget used if you don't specify .. class:: ImageField(**kwargs) - * Default widget: ``ClearableFileInput`` + * Default widget: :class:`ClearableFileInput` * Empty value: ``None`` * Normalizes to: An ``UploadedFile`` object that wraps the file content and file name into a single object. @@ -621,7 +623,8 @@ For each field, we describe the default widget used if you don't specify .. class:: IntegerField(**kwargs) - * Default widget: ``TextInput`` + * Default widget: :class:`NumberInput` when :attr:`Field.localize` is + ``False``, else :class:`TextInput`. * Empty value: ``None`` * Normalizes to: A Python integer or long integer. * Validates that the given value is an integer. Leading and trailing @@ -644,7 +647,7 @@ For each field, we describe the default widget used if you don't specify .. class:: IPAddressField(**kwargs) - * Default widget: ``TextInput`` + * Default widget: :class:`TextInput` * Empty value: ``''`` (an empty string) * Normalizes to: A Unicode object. * Validates that the given value is a valid IPv4 address, using a regular @@ -654,13 +657,11 @@ For each field, we describe the default widget used if you don't specify ``GenericIPAddressField`` ~~~~~~~~~~~~~~~~~~~~~~~~~ -.. versionadded:: 1.4 - .. class:: GenericIPAddressField(**kwargs) A field containing either an IPv4 or an IPv6 address. - * Default widget: ``TextInput`` + * Default widget: :class:`TextInput` * Empty value: ``''`` (an empty string) * Normalizes to: A Unicode object. IPv6 addresses are normalized as described below. @@ -693,7 +694,7 @@ For each field, we describe the default widget used if you don't specify .. class:: MultipleChoiceField(**kwargs) - * Default widget: ``SelectMultiple`` + * Default widget: :class:`SelectMultiple` * Empty value: ``[]`` (an empty list) * Normalizes to: A list of Unicode objects. * Validates that every value in the given list of values exists in the list @@ -713,7 +714,7 @@ For each field, we describe the default widget used if you don't specify Just like a :class:`MultipleChoiceField`, except :class:`TypedMultipleChoiceField` takes two extra arguments, ``coerce`` and ``empty_value``. - * Default widget: ``SelectMultiple`` + * Default widget: :class:`SelectMultiple` * Empty value: Whatever you've given as ``empty_value`` * Normalizes to: A list of values of the type provided by the ``coerce`` argument. @@ -731,7 +732,7 @@ For each field, we describe the default widget used if you don't specify .. class:: NullBooleanField(**kwargs) - * Default widget: ``NullBooleanSelect`` + * Default widget: :class:`NullBooleanSelect` * Empty value: ``None`` * Normalizes to: A Python ``True``, ``False`` or ``None`` value. * Validates nothing (i.e., it never raises a ``ValidationError``). @@ -741,7 +742,7 @@ For each field, we describe the default widget used if you don't specify .. class:: RegexField(**kwargs) - * Default widget: ``TextInput`` + * Default widget: :class:`TextInput` * Empty value: ``''`` (an empty string) * Normalizes to: A Unicode object. * Validates that the given value matches against a certain regular @@ -768,7 +769,7 @@ For each field, we describe the default widget used if you don't specify .. class:: SlugField(**kwargs) - * Default widget: ``TextInput`` + * Default widget: :class:`TextInput` * Empty value: ``''`` (an empty string) * Normalizes to: A Unicode object. * Validates that the given value contains only letters, numbers, @@ -783,7 +784,7 @@ For each field, we describe the default widget used if you don't specify .. class:: TimeField(**kwargs) - * Default widget: ``TextInput`` + * Default widget: :class:`TextInput` * Empty value: ``None`` * Normalizes to: A Python ``datetime.time`` object. * Validates that the given value is either a ``datetime.time`` or string @@ -807,7 +808,7 @@ For each field, we describe the default widget used if you don't specify .. class:: URLField(**kwargs) - * Default widget: ``TextInput`` + * Default widget: :class:`URLInput` * Empty value: ``''`` (an empty string) * Normalizes to: A Unicode object. * Validates that the given value is a valid URL. @@ -829,7 +830,7 @@ Slightly complex built-in ``Field`` classes .. class:: ComboField(**kwargs) - * Default widget: ``TextInput`` + * Default widget: :class:`TextInput` * Empty value: ``''`` (an empty string) * Normalizes to: A Unicode object. * Validates that the given value against each of the fields specified @@ -856,7 +857,7 @@ Slightly complex built-in ``Field`` classes .. class:: MultiValueField(fields=(), **kwargs) - * Default widget: ``TextInput`` + * Default widget: :class:`TextInput` * Empty value: ``''`` (an empty string) * Normalizes to: the type returned by the ``compress`` method of the subclass. * Validates that the given value against each of the fields specified @@ -885,7 +886,7 @@ Slightly complex built-in ``Field`` classes .. attribute:: MultiValueField.widget Must be a subclass of :class:`django.forms.MultiWidget`. - Default value is :class:`~django.forms.widgets.TextInput`, which + Default value is :class:`~django.forms.TextInput`, which probably is not very useful in this case. .. method:: compress(data_list) @@ -902,7 +903,7 @@ Slightly complex built-in ``Field`` classes .. class:: SplitDateTimeField(**kwargs) - * Default widget: ``SplitDateTimeWidget`` + * Default widget: :class:`SplitDateTimeWidget` * Empty value: ``None`` * Normalizes to: A Python ``datetime.datetime`` object. * Validates that the given value is a ``datetime.datetime`` or string @@ -945,7 +946,7 @@ objects (in the case of ``ModelMultipleChoiceField``) into the .. class:: ModelChoiceField(**kwargs) - * Default widget: ``Select`` + * Default widget: :class:`Select` * Empty value: ``None`` * Normalizes to: A model instance. * Validates that the given id exists in the queryset. @@ -1000,7 +1001,7 @@ objects (in the case of ``ModelMultipleChoiceField``) into the .. class:: ModelMultipleChoiceField(**kwargs) - * Default widget: ``SelectMultiple`` + * Default widget: :class:`SelectMultiple` * Empty value: An empty ``QuerySet`` (self.queryset.none()) * Normalizes to: A ``QuerySet`` of model instances. * Validates that every id in the given list of values exists in the diff --git a/docs/ref/forms/formsets.txt b/docs/ref/forms/formsets.txt new file mode 100644 index 0000000000..0ab2590fce --- /dev/null +++ b/docs/ref/forms/formsets.txt @@ -0,0 +1,16 @@ +==================== +Formset Functions +==================== + +.. module:: django.forms.formsets + :synopsis: Django's functions for building formsets. + +.. function:: formset_factory(form, formset=BaseFormSet, extra=1, can_order=False, can_delete=False, max_num=None, validate_max=False) + + Returns a ``FormSet`` class for the given ``form`` class. + + See :ref:`formsets` for example usage. + + .. versionchanged:: 1.6 + + The ``validate_max`` parameter was added. diff --git a/docs/ref/forms/index.txt b/docs/ref/forms/index.txt index 866afed6dc..e6edc88ca1 100644 --- a/docs/ref/forms/index.txt +++ b/docs/ref/forms/index.txt @@ -9,5 +9,7 @@ Detailed form API reference. For introductory material, see :doc:`/topics/forms/ api fields + models + formsets widgets validation diff --git a/docs/ref/forms/models.txt b/docs/ref/forms/models.txt new file mode 100644 index 0000000000..dd0a422fd0 --- /dev/null +++ b/docs/ref/forms/models.txt @@ -0,0 +1,60 @@ +==================== +Model Form Functions +==================== + +.. module:: django.forms.models + :synopsis: Django's functions for building model forms and formsets. + +.. function:: modelform_factory(model, form=ModelForm, fields=None, exclude=None, formfield_callback=None, widgets=None) + + Returns a :class:`~django.forms.ModelForm` class for the given ``model``. + You can optionally pass a ``form`` argument to use as a starting point for + constructing the ``ModelForm``. + + ``fields`` is an optional list of field names. If provided, only the named + fields will be included in the returned fields. + + ``exclude`` is an optional list of field names. If provided, the named + fields will be excluded from the returned fields, even if they are listed + in the ``fields`` argument. + + ``widgets`` is a dictionary of model field names mapped to a widget. + + ``formfield_callback`` is a callable that takes a model field and returns + a form field. + + See :ref:`modelforms-factory` for example usage. + +.. function:: modelformset_factory(model, form=ModelForm, formfield_callback=None, formset=BaseModelFormSet, extra=1, can_delete=False, can_order=False, max_num=None, fields=None, exclude=None, widgets=None, validate_max=False) + + Returns a ``FormSet`` class for the given ``model`` class. + + Arguments ``model``, ``form``, ``fields``, ``exclude``, + ``formfield_callback`` and ``widgets`` are all passed through to + :func:`~django.forms.models.modelform_factory`. + + Arguments ``formset``, ``extra``, ``max_num``, ``can_order``, + ``can_delete`` and ``validate_max`` are passed through to + :func:`~django.forms.formsets.formset_factory`. See :ref:`formsets` for + details. + + See :ref:`model-formsets` for example usage. + + .. versionchanged:: 1.6 + + The ``widgets`` and the ``validate_max`` parameters were added. + +.. function:: inlineformset_factory(parent_model, model, form=ModelForm, formset=BaseInlineFormSet, fk_name=None, fields=None, exclude=None, extra=3, can_order=False, can_delete=True, max_num=None, formfield_callback=None, widgets=None, validate_max=False) + + Returns an ``InlineFormSet`` using :func:`modelformset_factory` with + defaults of ``formset=BaseInlineFormSet``, ``can_delete=True``, and + ``extra=3``. + + If your model has more than one :class:`~django.db.models.ForeignKey` to + the ``parent_model``, you must specify a ``fk_name``. + + See :ref:`inline-formsets` for example usage. + + .. versionchanged:: 1.6 + + The ``widgets`` and the ``validate_max`` parameters were added. diff --git a/docs/ref/forms/validation.txt b/docs/ref/forms/validation.txt index e89bce748f..978c985b55 100644 --- a/docs/ref/forms/validation.txt +++ b/docs/ref/forms/validation.txt @@ -181,24 +181,20 @@ the field's ``validators`` argument, or defined on the Field class itself with the ``default_validators`` attribute. Simple validators can be used to validate values inside the field, let's have -a look at Django's ``EmailField``:: +a look at Django's ``SlugField``:: - class EmailField(CharField): - default_error_messages = { - 'invalid': _('Enter a valid email address.'), - } - default_validators = [validators.validate_email] + class SlugField(CharField): + default_validators = [validators.validate_slug] -As you can see, ``EmailField`` is just a ``CharField`` with customized error -message and a validator that validates email addresses. This can also be done -on field definition so:: +As you can see, ``SlugField`` is just a ``CharField`` with a customized +validator that validates that submitted text obeys to some character rules. +This can also be done on field definition so:: - email = forms.EmailField() + slug = forms.SlugField() is equivalent to:: - email = forms.CharField(validators=[validators.validate_email], - error_messages={'invalid': _('Enter a valid email address.')}) + slug = forms.CharField(validators=[validators.validate_slug]) Form field default cleaning diff --git a/docs/ref/forms/widgets.txt b/docs/ref/forms/widgets.txt index a0ef0731ad..514e8b3dc0 100644 --- a/docs/ref/forms/widgets.txt +++ b/docs/ref/forms/widgets.txt @@ -49,8 +49,8 @@ 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`:: +: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 @@ -139,7 +139,7 @@ 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>Url:</th><td><input type="url" 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 @@ -160,7 +160,7 @@ 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>Url:</th><td><input type="url" name="url"/></td></tr> <tr><th>Comment:</th><td><input type="text" name="comment" size="40"/></td></tr> .. _styling-widget-classes: @@ -222,7 +222,7 @@ foundation for custom widgets. .. class:: MultiWidget(widgets, attrs=None) A widget that is composed of multiple widgets. - :class:`~django.forms.widgets.MultiWidget` works hand in hand with the + :class:`~django.forms.MultiWidget` works hand in hand with the :class:`~django.forms.MultiValueField`. :class:`MultiWidget` has one required argument: @@ -246,8 +246,8 @@ foundation for custom widgets. 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` value into a list with date and time split into two - separate values:: + :class:`~datetime.datetime` value into a list with date and time split + into two separate values:: class SplitDateTimeWidget(MultiWidget): @@ -279,15 +279,10 @@ foundation for custom widgets. * 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). + If ``value`` is a list, the output of :meth:`~MultiWidget.render` will + be a concatenation of rendered child widgets. If ``value`` is not a + list, it will first be processed by the method + :meth:`~MultiWidget.decompress()` to create the list and then rendered. When ``render()`` executes its HTML rendering, each value in the list is rendered with the corresponding widget -- the first value is @@ -392,7 +387,38 @@ These widgets make use of the HTML elements ``input`` and ``textarea``. .. class:: TextInput - Text input: ``<input type='text' ...>`` + Text input: ``<input type="text" ...>`` + +``NumberInput`` +~~~~~~~~~~~~~~~ + +.. class:: NumberInput + + .. versionadded:: 1.6 + + Text input: ``<input type="number" ...>`` + + Beware that not all browsers support entering localized numbers in + ``number`` input types. Django itself avoids using them for fields having + their :attr:`~django.forms.Field.localize` property to ``True``. + +``EmailInput`` +~~~~~~~~~~~~~~ + +.. class:: EmailInput + + .. versionadded:: 1.6 + + Text input: ``<input type="email" ...>`` + +``URLInput`` +~~~~~~~~~~~~ + +.. class:: URLInput + + .. versionadded:: 1.6 + + Text input: ``<input type="url" ...>`` ``PasswordInput`` ~~~~~~~~~~~~~~~~~ @@ -508,9 +534,9 @@ Selector and checkbox widgets .. 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`. + 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`` ~~~~~~~~~~~~~~~~~~~~~ @@ -542,8 +568,6 @@ Selector and checkbox widgets ... </ul> - .. 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: @@ -609,6 +633,11 @@ Selector and checkbox widgets If you decide not to loop over the radio buttons -- e.g., if your template simply includes ``{{ myform.beatles }}`` -- they'll be output in a ``<ul>`` with ``<li>`` tags, as above. +.. versionchanged:: 1.6 + +The outer ``<ul>`` container will now receive the ``id`` attribute defined on +the widget. + ``CheckboxSelectMultiple`` ~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -624,6 +653,14 @@ Selector and checkbox widgets ... </ul> +.. versionchanged:: 1.6 + +The outer ``<ul>`` container will now receive the ``id`` attribute defined on +the widget. + +Like :class:`RadioSelect`, you can now loop over the individual checkboxes making +up the lists. See the documentation of :class:`RadioSelect` for more details. + .. _file-upload-widgets: File upload widgets @@ -662,9 +699,9 @@ Composite widgets .. 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`. + 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`. ``SplitDateTimeWidget`` ~~~~~~~~~~~~~~~~~~~~~~~ |
