1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Refs #32339 -- Updated Form API docs to prefer as_div() output style.

This commit is contained in:
David Smith 2022-12-07 09:49:45 +00:00 committed by GitHub
parent d8c8761901
commit 9ac97e7eb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -342,23 +342,23 @@ attribute::
>>> f.fields['name'] >>> f.fields['name']
<django.forms.fields.CharField object at 0x7ffaac6324d0> <django.forms.fields.CharField object at 0x7ffaac6324d0>
You can alter the field of :class:`Form` instance to change the way it is You can alter the field and :class:`.BoundField` of :class:`Form` instance to
presented in the form:: change the way it is presented in the form::
>>> f.as_table().split('\n')[0] >>> f.as_div().split("</div>")[0]
'<tr><th>Name:</th><td><input name="name" type="text" value="instance" required></td></tr>' '<div><label for="id_subject">Subject:</label><input type="text" name="subject" maxlength="100" required id="id_subject">'
>>> f.fields['name'].label = "Username" >>> f["subject"].label = "Topic"
>>> f.as_table().split('\n')[0] >>> f.as_div().split("</div>")[0]
'<tr><th>Username:</th><td><input name="name" type="text" value="instance" required></td></tr>' '<div><label for="id_subject">Topic:</label><input type="text" name="subject" maxlength="100" required id="id_subject">'
Beware not to alter the ``base_fields`` attribute because this modification Beware not to alter the ``base_fields`` attribute because this modification
will influence all subsequent ``ContactForm`` instances within the same Python will influence all subsequent ``ContactForm`` instances within the same Python
process:: process::
>>> f.base_fields['name'].label = "Username" >>> f.base_fields["subject"].label_suffix = "?"
>>> another_f = CommentForm(auto_id=False) >>> another_f = CommentForm(auto_id=False)
>>> another_f.as_table().split('\n')[0] >>> f.as_div().split("</div>")[0]
'<tr><th>Username:</th><td><input name="name" type="text" value="class" required></td></tr>' '<div><label for="id_subject">Subject?</label><input type="text" name="subject" maxlength="100" required id="id_subject">'
Accessing "clean" data Accessing "clean" data
====================== ======================
@ -782,42 +782,22 @@ If ``auto_id`` is ``False``, then the form output will not include ``<label>``
tags nor ``id`` attributes:: tags nor ``id`` attributes::
>>> f = ContactForm(auto_id=False) >>> f = ContactForm(auto_id=False)
>>> print(f.as_table()) >>> print(f.as_div())
<tr><th>Subject:</th><td><input type="text" name="subject" maxlength="100" required></td></tr> <div>Subject:<input type="text" name="subject" maxlength="100" required></div>
<tr><th>Message:</th><td><input type="text" name="message" required></td></tr> <div>Message:<textarea name="message" cols="40" rows="10" required></textarea></div>
<tr><th>Sender:</th><td><input type="email" name="sender" required></td></tr> <div>Sender:<input type="email" name="sender" required></div>
<tr><th>Cc myself:</th><td><input type="checkbox" name="cc_myself"></td></tr> <div>Cc myself:<input type="checkbox" name="cc_myself"></div>
>>> print(f.as_ul())
<li>Subject: <input type="text" name="subject" maxlength="100" required></li>
<li>Message: <input type="text" name="message" required></li>
<li>Sender: <input type="email" name="sender" required></li>
<li>Cc myself: <input type="checkbox" name="cc_myself"></li>
>>> print(f.as_p())
<p>Subject: <input type="text" name="subject" maxlength="100" required></p>
<p>Message: <input type="text" name="message" required></p>
<p>Sender: <input type="email" name="sender" required></p>
<p>Cc myself: <input type="checkbox" name="cc_myself"></p>
If ``auto_id`` is set to ``True``, then the form output *will* include If ``auto_id`` is set to ``True``, then the form output *will* include
``<label>`` tags and will use the field name as its ``id`` for each form ``<label>`` tags and will use the field name as its ``id`` for each form
field:: field::
>>> f = ContactForm(auto_id=True) >>> f = ContactForm(auto_id=True)
>>> print(f.as_table()) >>> print(f.as_div())
<tr><th><label for="subject">Subject:</label></th><td><input id="subject" type="text" name="subject" maxlength="100" required></td></tr> <div><label for="subject">Subject:</label><input type="text" name="subject" maxlength="100" required id="subject"></div>
<tr><th><label for="message">Message:</label></th><td><input type="text" name="message" id="message" required></td></tr> <div><label for="message">Message:</label><textarea name="message" cols="40" rows="10" required id="message"></textarea></div>
<tr><th><label for="sender">Sender:</label></th><td><input type="email" name="sender" id="sender" required></td></tr> <div><label for="sender">Sender:</label><input type="email" name="sender" required id="sender"></div>
<tr><th><label for="cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="cc_myself"></td></tr> <div><label for="cc_myself">Cc myself:</label><input type="checkbox" name="cc_myself" id="cc_myself"></div>
>>> print(f.as_ul())
<li><label for="subject">Subject:</label> <input id="subject" type="text" name="subject" maxlength="100" required></li>
<li><label for="message">Message:</label> <input type="text" name="message" id="message" required></li>
<li><label for="sender">Sender:</label> <input type="email" name="sender" id="sender" required></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" required></p>
<p><label for="message">Message:</label> <input type="text" name="message" id="message" required></p>
<p><label for="sender">Sender:</label> <input type="email" name="sender" id="sender" required></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'``, If ``auto_id`` is set to a string containing the format character ``'%s'``,
then the form output will include ``<label>`` tags, and will generate ``id`` then the form output will include ``<label>`` tags, and will generate ``id``
@ -826,21 +806,11 @@ attributes based on the format string. For example, for a format string
``'field_subject'``. Continuing our example:: ``'field_subject'``. Continuing our example::
>>> f = ContactForm(auto_id='id_for_%s') >>> f = ContactForm(auto_id='id_for_%s')
>>> print(f.as_table()) >>> print(f.as_div())
<tr><th><label for="id_for_subject">Subject:</label></th><td><input id="id_for_subject" type="text" name="subject" maxlength="100" required></td></tr> <div><label for="id_for_subject">Subject:</label><input type="text" name="subject" maxlength="100" required id="id_for_subject"></div>
<tr><th><label for="id_for_message">Message:</label></th><td><input type="text" name="message" id="id_for_message" required></td></tr> <div><label for="id_for_message">Message:</label><textarea name="message" cols="40" rows="10" required id="id_for_message"></textarea></div>
<tr><th><label for="id_for_sender">Sender:</label></th><td><input type="email" name="sender" id="id_for_sender" required></td></tr> <div><label for="id_for_sender">Sender:</label><input type="email" name="sender" required id="id_for_sender"></div>
<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> <div><label for="id_for_cc_myself">Cc myself:</label><input type="checkbox" name="cc_myself" id="id_for_cc_myself"></div>
>>> print(f.as_ul())
<li><label for="id_for_subject">Subject:</label> <input id="id_for_subject" type="text" name="subject" maxlength="100" required></li>
<li><label for="id_for_message">Message:</label> <input type="text" name="message" id="id_for_message" required></li>
<li><label for="id_for_sender">Sender:</label> <input type="email" name="sender" id="id_for_sender" required></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" required></p>
<p><label for="id_for_message">Message:</label> <input type="text" name="message" id="id_for_message" required></p>
<p><label for="id_for_sender">Sender:</label> <input type="email" name="sender" id="id_for_sender" required></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 If ``auto_id`` is set to any other true value -- such as a string that doesn't
include ``%s`` -- then the library will act as if ``auto_id`` is ``True``. include ``%s`` -- then the library will act as if ``auto_id`` is ``True``.
@ -856,17 +826,17 @@ It's possible to customize that character, or omit it entirely, using the
``label_suffix`` parameter:: ``label_suffix`` parameter::
>>> f = ContactForm(auto_id='id_for_%s', label_suffix='') >>> f = ContactForm(auto_id='id_for_%s', label_suffix='')
>>> print(f.as_ul()) >>> print(f.as_div())
<li><label for="id_for_subject">Subject</label> <input id="id_for_subject" type="text" name="subject" maxlength="100" required></li> <div><label for="id_for_subject">Subject</label><input type="text" name="subject" maxlength="100" required id="id_for_subject"></div>
<li><label for="id_for_message">Message</label> <input type="text" name="message" id="id_for_message" required></li> <div><label for="id_for_message">Message</label><textarea name="message" cols="40" rows="10" required id="id_for_message"></textarea></div>
<li><label for="id_for_sender">Sender</label> <input type="email" name="sender" id="id_for_sender" required></li> <div><label for="id_for_sender">Sender</label><input type="email" name="sender" required id="id_for_sender"></div>
<li><label for="id_for_cc_myself">Cc myself</label> <input type="checkbox" name="cc_myself" id="id_for_cc_myself"></li> <div><label for="id_for_cc_myself">Cc myself</label><input type="checkbox" name="cc_myself" id="id_for_cc_myself"></div>
>>> f = ContactForm(auto_id='id_for_%s', label_suffix=' ->') >>> f = ContactForm(auto_id='id_for_%s', label_suffix=' ->')
>>> print(f.as_ul()) >>> print(f.as_div())
<li><label for="id_for_subject">Subject -></label> <input id="id_for_subject" type="text" name="subject" maxlength="100" required></li> <div><label for="id_for_subject">Subject:</label><input type="text" name="subject" maxlength="100" required id="id_for_subject"></div>
<li><label for="id_for_message">Message -></label> <input type="text" name="message" id="id_for_message" required></li> <div><label for="id_for_message">Message -&gt;</label><textarea name="message" cols="40" rows="10" required id="id_for_message"></textarea></div>
<li><label for="id_for_sender">Sender -></label> <input type="email" name="sender" id="id_for_sender" required></li> <div><label for="id_for_sender">Sender -&gt;</label><input type="email" name="sender" required id="id_for_sender"></div>
<li><label for="id_for_cc_myself">Cc myself -></label> <input type="checkbox" name="cc_myself" id="id_for_cc_myself"></li> <div><label for="id_for_cc_myself">Cc myself -&gt;</label><input type="checkbox" name="cc_myself" id="id_for_cc_myself"></div>
Note that the label suffix is added only if the last character of the Note that the label suffix is added only if the last character of the
label isn't a punctuation character (in English, those are ``.``, ``!``, ``?`` label isn't a punctuation character (in English, those are ``.``, ``!``, ``?``
@ -953,20 +923,25 @@ method you're using::
... 'sender': 'invalid email address', ... 'sender': 'invalid email address',
... 'cc_myself': True} ... 'cc_myself': True}
>>> f = ContactForm(data, auto_id=False) >>> f = ContactForm(data, auto_id=False)
>>> print(f.as_div())
<div>Subject:<ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="subject" maxlength="100" required></div>
<div>Message:<textarea name="message" cols="40" rows="10" required>Hi there</textarea></div>
<div>Sender:<ul class="errorlist"><li>Enter a valid email address.</li></ul><input type="email" name="sender" value="invalid email address" required></div>
<div>Cc myself:<input type="checkbox" name="cc_myself" checked></div>
>>> print(f.as_table()) >>> 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" required></td></tr> <tr><th>Subject:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="subject" maxlength="100" required></td></tr>
<tr><th>Message:</th><td><input type="text" name="message" value="Hi there" required></td></tr> <tr><th>Message:</th><td><textarea name="message" cols="40" rows="10" required></textarea></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" required></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" required></td></tr>
<tr><th>Cc myself:</th><td><input checked type="checkbox" name="cc_myself"></td></tr> <tr><th>Cc myself:</th><td><input checked type="checkbox" name="cc_myself"></td></tr>
>>> print(f.as_ul()) >>> print(f.as_ul())
<li><ul class="errorlist"><li>This field is required.</li></ul>Subject: <input type="text" name="subject" maxlength="100" required></li> <li><ul class="errorlist"><li>This field is required.</li></ul>Subject: <input type="text" name="subject" maxlength="100" required></li>
<li>Message: <input type="text" name="message" value="Hi there" required></li> <li>Message: <textarea name="message" cols="40" rows="10" required></textarea></li>
<li><ul class="errorlist"><li>Enter a valid email address.</li></ul>Sender: <input type="email" name="sender" value="invalid email address" required></li> <li><ul class="errorlist"><li>Enter a valid email address.</li></ul>Sender: <input type="email" name="sender" value="invalid email address" required></li>
<li>Cc myself: <input checked type="checkbox" name="cc_myself"></li> <li>Cc myself: <input checked type="checkbox" name="cc_myself"></li>
>>> print(f.as_p()) >>> print(f.as_p())
<p><ul class="errorlist"><li>This field is required.</li></ul></p> <p><ul class="errorlist"><li>This field is required.</li></ul></p>
<p>Subject: <input type="text" name="subject" maxlength="100" required></p> <p>Subject: <input type="text" name="subject" maxlength="100" required></p>
<p>Message: <input type="text" name="message" value="Hi there" required></p> <p>Message: <textarea name="message" cols="40" rows="10" required></textarea></p>
<p><ul class="errorlist"><li>Enter a valid email address.</li></ul></p> <p><ul class="errorlist"><li>Enter a valid email address.</li></ul></p>
<p>Sender: <input type="email" name="sender" value="invalid email address" required></p> <p>Sender: <input type="email" name="sender" value="invalid email address" required></p>
<p>Cc myself: <input checked type="checkbox" name="cc_myself"></p> <p>Cc myself: <input checked type="checkbox" name="cc_myself"></p>
@ -1466,12 +1441,12 @@ fields are ordered first::
>>> class ContactFormWithPriority(ContactForm): >>> class ContactFormWithPriority(ContactForm):
... priority = forms.CharField() ... priority = forms.CharField()
>>> f = ContactFormWithPriority(auto_id=False) >>> f = ContactFormWithPriority(auto_id=False)
>>> print(f.as_ul()) >>> print(f.as_div())
<li>Subject: <input type="text" name="subject" maxlength="100" required></li> <div>Subject:<input type="text" name="subject" maxlength="100" required></div>
<li>Message: <input type="text" name="message" required></li> <div>Message:<textarea name="message" cols="40" rows="10" required></textarea></div>
<li>Sender: <input type="email" name="sender" required></li> <div>Sender:<input type="email" name="sender" required></div>
<li>Cc myself: <input type="checkbox" name="cc_myself"></li> <div>Cc myself:<input type="checkbox" name="cc_myself"></div>
<li>Priority: <input type="text" name="priority" required></li> <div>Priority:<input type="text" name="priority" required></div>
It's possible to subclass multiple forms, treating forms as mixins. In this It's possible to subclass multiple forms, treating forms as mixins. In this
example, ``BeatleForm`` subclasses both ``PersonForm`` and ``InstrumentForm`` example, ``BeatleForm`` subclasses both ``PersonForm`` and ``InstrumentForm``
@ -1487,11 +1462,11 @@ classes::
>>> class BeatleForm(InstrumentForm, PersonForm): >>> class BeatleForm(InstrumentForm, PersonForm):
... haircut_type = forms.CharField() ... haircut_type = forms.CharField()
>>> b = BeatleForm(auto_id=False) >>> b = BeatleForm(auto_id=False)
>>> print(b.as_ul()) >>> print(b.as_div())
<li>First name: <input type="text" name="first_name" required></li> <div>First name:<input type="text" name="first_name" required></div>
<li>Last name: <input type="text" name="last_name" required></li> <div>Last name:<input type="text" name="last_name" required></div>
<li>Instrument: <input type="text" name="instrument" required></li> <div>Instrument:<input type="text" name="instrument" required></div>
<li>Haircut type: <input type="text" name="haircut_type" required></li> <div>Haircut type:<input type="text" name="haircut_type" required></div>
It's possible to declaratively remove a ``Field`` inherited from a parent class It's possible to declaratively remove a ``Field`` inherited from a parent class
by setting the name of the field to ``None`` on the subclass. For example:: by setting the name of the field to ``None`` on the subclass. For example::
@ -1520,12 +1495,12 @@ You can put several Django forms inside one ``<form>`` tag. To give each
>>> mother = PersonForm(prefix="mother") >>> mother = PersonForm(prefix="mother")
>>> father = PersonForm(prefix="father") >>> father = PersonForm(prefix="father")
>>> print(mother.as_ul()) >>> print(mother.as_div())
<li><label for="id_mother-first_name">First name:</label> <input type="text" name="mother-first_name" id="id_mother-first_name" required></li> <div><label for="id_mother-first_name">First name:</label><input type="text" name="mother-first_name" required id="id_mother-first_name"></div>
<li><label for="id_mother-last_name">Last name:</label> <input type="text" name="mother-last_name" id="id_mother-last_name" required></li> <div><label for="id_mother-last_name">Last name:</label><input type="text" name="mother-last_name" required id="id_mother-last_name"></div>
>>> print(father.as_ul()) >>> print(father.as_div())
<li><label for="id_father-first_name">First name:</label> <input type="text" name="father-first_name" id="id_father-first_name" required></li> <div><label for="id_father-first_name">First name:</label><input type="text" name="father-first_name" required id="id_father-first_name"></div>
<li><label for="id_father-last_name">Last name:</label> <input type="text" name="father-last_name" id="id_father-last_name" required></li> <div><label for="id_father-last_name">Last name:</label><input type="text" name="father-last_name" required id="id_father-last_name"></div>
The prefix can also be specified on the form class:: The prefix can also be specified on the form class::