From fde946daffbb007a7d033945677cc8e9e475d516 Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Tue, 3 May 2022 16:07:01 +0200 Subject: Refs #32339 -- Restructured outputting HTML form docs. In the topic doc, promoted the Reusable form templates section. In the reference, re-grouped and promoted the default __str__() rendering path, and then gathered the various as_*() helpers subsequently. Thanks to David Smith for review. --- docs/ref/forms/api.txt | 128 ++++++++++++++++++++++++++++++------------------- 1 file changed, 78 insertions(+), 50 deletions(-) (limited to 'docs/ref/forms') diff --git a/docs/ref/forms/api.txt b/docs/ref/forms/api.txt index a6b4d11f4a..8530af2611 100644 --- a/docs/ref/forms/api.txt +++ b/docs/ref/forms/api.txt @@ -520,9 +520,15 @@ 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. -``template_name`` +Default rendering ----------------- +The default rendering when you ``print`` a form uses the following methods and +attributes. + +``template_name`` +~~~~~~~~~~~~~~~~~ + .. versionadded:: 4.0 .. attribute:: Form.template_name @@ -540,8 +546,42 @@ class. In older versions ``template_name`` defaulted to the string value ``'django/forms/default.html'``. +``render()`` +~~~~~~~~~~~~ + +.. versionadded:: 4.0 + +.. method:: Form.render(template_name=None, context=None, renderer=None) + +The render method is called by ``__str__`` as well as the +:meth:`.Form.as_table`, :meth:`.Form.as_p`, and :meth:`.Form.as_ul` methods. +All arguments are optional and default to: + +* ``template_name``: :attr:`.Form.template_name` +* ``context``: Value returned by :meth:`.Form.get_context` +* ``renderer``: Value returned by :attr:`.Form.default_renderer` + +By passing ``template_name`` you can customize the template used for just a +single call. + +``get_context()`` +~~~~~~~~~~~~~~~~~ + +.. versionadded:: 4.0 + +.. method:: Form.get_context() + +Return the template context for rendering the form. + +The available context is: + +* ``form``: The bound form. +* ``fields``: All bound fields, except the hidden fields. +* ``hidden_fields``: All hidden bound fields. +* ``errors``: All non field related or hidden field related form errors. + ``template_name_label`` ------------------------ +~~~~~~~~~~~~~~~~~~~~~~~ .. versionadded:: 4.0 @@ -552,15 +592,32 @@ The template used to render a field's ``
``:: +``as_table()`` renders the form as an HTML ``
``:: >>> f = ContactForm() >>> f.as_table() @@ -611,37 +670,6 @@ forms ``template_name_table`` attribute, by default this template is -``get_context()`` ------------------ - -.. versionadded:: 4.0 - -.. method:: Form.get_context() - -Return context for form rendering in a template. - -The available context is: - -* ``form``: The bound form. -* ``fields``: All bound fields, except the hidden fields. -* ``hidden_fields``: All hidden bound fields. -* ``errors``: All non field related or hidden field related form errors. - -``render()`` ------------- - -.. versionadded:: 4.0 - -.. method:: Form.render(template_name=None, context=None, renderer=None) - -The render method is called by ``__str__`` as well as the -:meth:`.Form.as_table`, :meth:`.Form.as_p`, and :meth:`.Form.as_ul` methods. -All arguments are optional and default to: - -* ``template_name``: :attr:`.Form.template_name` -* ``context``: Value returned by :meth:`.Form.get_context` -* ``renderer``: Value returned by :attr:`.Form.default_renderer` - .. _ref-forms-api-styling-form-rows: Styling required or erroneous form rows -- cgit v1.3