From 232b60a21b951bd16b8c95b34fcbcbf3ecd89fca Mon Sep 17 00:00:00 2001 From: David Smith Date: Sun, 12 Feb 2023 13:20:05 +0000 Subject: [PATCH] Refs #32339 -- Updated docs to reflect default
style form rendering in Django 5.0. Follow up to 98756c685ee173bbd43f21ed0553f808be835ce5. --- docs/ref/forms/api.txt | 79 ++++++++++++-------------------- docs/ref/forms/fields.txt | 48 ++++++++----------- docs/ref/forms/widgets.txt | 16 +++---- docs/topics/forms/formsets.txt | 74 +++++++++++++++--------------- docs/topics/forms/modelforms.txt | 16 +++---- 5 files changed, 102 insertions(+), 131 deletions(-) diff --git a/docs/ref/forms/api.txt b/docs/ref/forms/api.txt index 8a18e1b5da..3a9ea81723 100644 --- a/docs/ref/forms/api.txt +++ b/docs/ref/forms/api.txt @@ -276,9 +276,9 @@ precedence: ... comment = forms.CharField() >>> f = CommentForm(initial={'name': 'instance'}, auto_id=False) >>> print(f) - Name: - Url: - Comment: +
Name:
+
Url:
+
Comment:
.. method:: Form.get_initial_for_field(field, field_name) @@ -500,10 +500,10 @@ The second task of a ``Form`` object is to render itself as HTML. To do so, >>> f = ContactForm() >>> print(f) - - - - +
+
+
+
If the form is bound to data, the HTML output will include that data appropriately. For example, if a field is represented by an @@ -519,16 +519,14 @@ include ``checked`` if appropriate: ... 'cc_myself': True} >>> f = ContactForm(data) >>> print(f) - - - - +
+
+
+
-This default output is a two-column HTML table, with a ```` for each field. -Notice the following: +This default output wraps each field with a ``
``. Notice the following: -* For flexibility, the output does *not* include the ```` and - ``
`` tags, nor does it include the ``
`` and ``
`` +* For flexibility, the output does *not* include the ``
`` and ``
`` tags or an ```` tag. It's your job to do that. * Each field type has a default HTML representation. ``CharField`` is @@ -556,7 +554,7 @@ Notice the following: it uses boolean attributes such as ``checked`` rather than the XHTML style of ``checked='checked'``. -Although ```` output is the default output style when you ``print`` a +Although ``
`` output is the default output style when you ``print`` a form, other output styles are available. Each style is available as a method on a form object, and each rendering method returns a string. @@ -584,7 +582,7 @@ class. .. method:: Form.render(template_name=None, context=None, renderer=None) -The render method is called by ``__str__`` as well as the +The render method is called by ``__str__`` as well as the :meth:`.Form.as_div`, :meth:`.Form.as_table`, :meth:`.Form.as_p`, and :meth:`.Form.as_ul` methods. All arguments are optional and default to: @@ -779,11 +777,11 @@ classes, as needed. The HTML will look something like: .. code-block:: pycon >>> f = ContactForm(data) - >>> print(f.as_table()) -
- - - - >>> print(f.as_ul()) -
    • This field is required.
    Subject:
  • -
  • Message:
  • -
    • Enter a valid email address.
    Sender:
  • -
  • Cc myself:
  • - >>> print(f.as_p()) -

    • This field is required.

    -

    Subject:

    -

    Message:

    -

    • Enter a valid email address.

    -

    Sender:

    -

    Cc myself:

    .. _ref-forms-error-list-format: @@ -1515,7 +1496,7 @@ fields are ordered first: >>> class ContactFormWithPriority(ContactForm): ... priority = forms.CharField() >>> f = ContactFormWithPriority(auto_id=False) - >>> print(f.as_div()) + >>> print(f)
    Subject:
    Message:
    Sender:
    @@ -1538,7 +1519,7 @@ classes: >>> class BeatleForm(InstrumentForm, PersonForm): ... haircut_type = forms.CharField() >>> b = BeatleForm(auto_id=False) - >>> print(b.as_div()) + >>> print(b)
    First name:
    Last name:
    Instrument:
    @@ -1575,10 +1556,10 @@ You can put several Django forms inside one ```` tag. To give each >>> mother = PersonForm(prefix="mother") >>> father = PersonForm(prefix="father") - >>> print(mother.as_div()) + >>> print(mother)
    - >>> print(father.as_div()) + >>> print(father)
    diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt index edf32ecdab..a7095ba5dc 100644 --- a/docs/ref/forms/fields.txt +++ b/docs/ref/forms/fields.txt @@ -129,9 +129,9 @@ We've specified ``auto_id=False`` to simplify the output: ... comment = forms.CharField() >>> f = CommentForm(auto_id=False) >>> print(f) - - - +
    Your name:
    +
    Your website:
    +
    Comment:
    ``label_suffix`` ---------------- @@ -148,10 +148,10 @@ The ``label_suffix`` argument lets you override the form's ... nationality = forms.CharField() ... captcha_answer = forms.IntegerField(label='2 + 2', label_suffix=' =') >>> f = ContactForm(label_suffix='?') - >>> print(f.as_p()) -

    -

    -

    + >>> print(f) +
    +
    +
    ``initial`` ----------- @@ -175,9 +175,9 @@ field is initialized to a particular value. For example: ... comment = forms.CharField() >>> f = CommentForm(auto_id=False) >>> print(f) - - - +
    Name:
    +
    Url:
    +
    Comment:
    You may be thinking, why not just pass a dictionary of the initial values as data when displaying the form? Well, if you do that, you'll trigger validation, @@ -192,9 +192,9 @@ and the HTML output will include any validation errors: >>> default_data = {'name': 'Your name', 'url': 'http://'} >>> f = CommentForm(default_data, auto_id=False) >>> print(f) - - - +
    Name:
    +
    Url:
    • Enter a valid URL.
    +
    Comment:
    • This field is required.
    This is why ``initial`` values are only displayed for unbound forms. For bound forms, the HTML output will use the bound data. @@ -225,7 +225,7 @@ Instead of a constant, you can also pass any callable: >>> class DateForm(forms.Form): ... day = forms.DateField(initial=datetime.date.today) >>> print(DateForm()) - +
    The callable will be evaluated only when the unbound form is displayed, not when it is defined. @@ -262,21 +262,11 @@ fields. We've specified ``auto_id=False`` to simplify the output: ... sender = forms.EmailField(help_text='A valid email address, please.') ... cc_myself = forms.BooleanField(required=False) >>> f = HelpTextContactForm(auto_id=False) - >>> print(f.as_table()) - - - - - >>> print(f.as_ul())) -
  • Subject: 100 characters max.
  • -
  • Message:
  • -
  • Sender: A valid email address, please.
  • -
  • Cc myself:
  • - >>> print(f.as_p()) -

    Subject: 100 characters max.

    -

    Message:

    -

    Sender: A valid email address, please.

    -

    Cc myself:

    + >>> print(f) +
    Subject:
    100 characters max.
    +
    Message:
    +
    Sender:
    A valid email address, please.
    +
    Cc myself:
    ``error_messages`` ------------------ diff --git a/docs/ref/forms/widgets.txt b/docs/ref/forms/widgets.txt index 45b154f753..43a579b4a5 100644 --- a/docs/ref/forms/widgets.txt +++ b/docs/ref/forms/widgets.txt @@ -144,10 +144,10 @@ provided for each widget will be rendered exactly the same: .. code-block:: pycon >>> f = CommentForm(auto_id=False) - >>> f.as_table() - - - + >>> print(f) +
    Name:
    +
    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 @@ -182,10 +182,10 @@ you can use the :attr:`Form.fields` attribute:: Django will then include the extra attributes in the rendered output: >>> f = CommentForm(auto_id=False) - >>> f.as_table() - - - + >>> print(f) +
    Name:
    +
    Url:
    +
    Comment:
    You can also set the HTML ``id`` using :attr:`~Widget.attrs`. See :attr:`BoundField.id_for_label` for an example. diff --git a/docs/topics/forms/formsets.txt b/docs/topics/forms/formsets.txt index d974d72f76..85c35dc2d0 100644 --- a/docs/topics/forms/formsets.txt +++ b/docs/topics/forms/formsets.txt @@ -33,9 +33,9 @@ in the formset and display them as you would with a regular form: >>> formset = ArticleFormSet() >>> for form in formset: - ... print(form.as_table()) - - + ... print(form) +
    +
    As you can see it only displayed one empty form. The number of empty forms that is displayed is controlled by the ``extra`` parameter. By default, @@ -77,13 +77,13 @@ example: ... ]) >>> for form in formset: - ... print(form.as_table()) - - - - - - + ... print(form) +
    +
    +
    +
    +
    +
    There are now a total of three forms showing above. One for the initial data that was passed in and two extra forms. Also note that we are passing in a @@ -113,9 +113,9 @@ gives you the ability to limit the number of forms the formset will display: >>> ArticleFormSet = formset_factory(ArticleForm, extra=2, max_num=1) >>> formset = ArticleFormSet() >>> for form in formset: - ... print(form.as_table()) - - + ... print(form) +
    +
    If the value of ``max_num`` is greater than the number of existing items in the initial data, up to ``extra`` additional blank forms will be added to the @@ -517,16 +517,16 @@ Lets you create a formset with the ability to order: ... {'title': 'Article #2', 'pub_date': datetime.date(2008, 5, 11)}, ... ]) >>> for form in formset: - ... print(form.as_table()) - - - - - - - - - + ... print(form) +
    +
    +
    +
    +
    +
    +
    +
    +
    This adds an additional field to each form. This new field is named ``ORDER`` and is an ``forms.IntegerField``. For the forms that came from the initial @@ -623,16 +623,16 @@ Lets you create a formset with the ability to select forms for deletion: ... {'title': 'Article #2', 'pub_date': datetime.date(2008, 5, 11)}, ... ]) >>> for form in formset: - ... print(form.as_table()) - - - - - - - - - + ... print(form) +
    +
    +
    +
    +
    +
    +
    +
    +
    Similar to ``can_order`` this adds a new field to each form named ``DELETE`` and is a ``forms.BooleanField``. When data comes through marking any of the @@ -755,10 +755,10 @@ fields/attributes of the order and deletion fields: >>> ArticleFormSet = formset_factory(ArticleForm, formset=BaseArticleFormSet) >>> formset = ArticleFormSet() >>> for form in formset: - ... print(form.as_table()) - - - + ... print(form) +
    +
    +
    .. _custom-formset-form-kwargs: diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt index 4242cd96e0..be871fd4b0 100644 --- a/docs/topics/forms/modelforms.txt +++ b/docs/topics/forms/modelforms.txt @@ -815,13 +815,13 @@ with the ``Author`` model. It works just like a regular formset: >>> formset = AuthorFormSet() >>> print(formset) - - + .. note:: @@ -1021,11 +1021,11 @@ so long as the total number of forms does not exceed ``max_num``: >>> AuthorFormSet = modelformset_factory(Author, fields=['name'], max_num=4, extra=2) >>> formset = AuthorFormSet(queryset=Author.objects.order_by('name')) >>> for form in formset: - ... print(form.as_table()) - - - - + ... print(form) +
    +
    +
    +
    A ``max_num`` value of ``None`` (the default) puts a high limit on the number of forms displayed (1000). In practice this is equivalent to no limit.
    ... -
    ... -
    ... -
    Subject:
    • This field is required.
    Message:
    Sender:
    • Enter a valid email address.
    Cc myself:
    Your name:
    Your website:
    Comment:
    Name:
    Url:
    Comment:
    Name:
    Url:
    • Enter a valid URL.
    Comment:
    • This field is required.
    Day:
    Subject:
    100 characters max.
    Message:
    Sender:
    A valid email address, please.
    Cc myself:
    Name:
    Url:
    Comment:
    Name:
    Url:
    Comment:
    +