@@ -520,9 +520,15 @@ Although ``<table>`` output is the default output style when you ``print`` a
520
520
form, other output styles are available. Each style is available as a method on
521
521
a form object, and each rendering method returns a string.
522
522
523
- ``template_name``
523
+ Default rendering
524
524
-----------------
525
525
526
+ The default rendering when you ``print`` a form uses the following methods and
527
+ attributes.
528
+
529
+ ``template_name``
530
+ ~~~~~~~~~~~~~~~~~
531
+
526
532
.. versionadded:: 4.0
527
533
528
534
.. attribute:: Form.template_name
@@ -540,8 +546,42 @@ class.
540
546
In older versions ``template_name`` defaulted to the string value
541
547
``'django/forms/default.html'``.
542
548
549
+ ``render()``
550
+ ~~~~~~~~~~~~
551
+
552
+ .. versionadded:: 4.0
553
+
554
+ .. method:: Form.render(template_name=None, context=None, renderer=None)
555
+
556
+ The render method is called by ``__str__`` as well as the
557
+ :meth:`.Form.as_table`, :meth:`.Form.as_p`, and :meth:`.Form.as_ul` methods.
558
+ All arguments are optional and default to:
559
+
560
+ * ``template_name``: :attr:`.Form.template_name`
561
+ * ``context``: Value returned by :meth:`.Form.get_context`
562
+ * ``renderer``: Value returned by :attr:`.Form.default_renderer`
563
+
564
+ By passing ``template_name`` you can customize the template used for just a
565
+ single call.
566
+
567
+ ``get_context()``
568
+ ~~~~~~~~~~~~~~~~~
569
+
570
+ .. versionadded:: 4.0
571
+
572
+ .. method:: Form.get_context()
573
+
574
+ Return the template context for rendering the form.
575
+
576
+ The available context is:
577
+
578
+ * ``form``: The bound form.
579
+ * ``fields``: All bound fields, except the hidden fields.
580
+ * ``hidden_fields``: All hidden bound fields.
581
+ * ``errors``: All non field related or hidden field related form errors.
582
+
543
583
``template_name_label``
544
- -----------------------
584
+ ~~~~~~~~~~~~~~~~~~~~~~~
545
585
546
586
.. versionadded:: 4.0
547
587
@@ -552,15 +592,32 @@ The template used to render a field's ``<label>``, used when calling
552
592
form by overriding this attribute or more generally by overriding the default
553
593
template, see also :ref:`overriding-built-in-form-templates`.
554
594
595
+ Output styles
596
+ -------------
597
+
598
+ As well as rendering the form directly, such as in a template with
599
+ ``{{ form }}``, the following helper functions serve as a proxy to
600
+ :meth:`Form.render` passing a particular ``template_name`` value.
601
+
602
+ These helpers are most useful in a template, where you need to override the
603
+ form renderer or form provided value but cannot pass the additional parameter
604
+ to :meth:`~Form.render`. For example, you can render a form as an unordered
605
+ list using ``{{ form.as_ul }}``.
606
+
607
+ Each helper pairs a form method with an attribute giving the appropriate
608
+ template name.
609
+
555
610
``as_p()``
556
- ----------
611
+ ~~~~~~~~~~
612
+
613
+ .. attribute:: Form.template_name_p
614
+
615
+ The template used by ``as_p()``. Default: ``'django/forms/p.html'``.
557
616
558
617
.. method:: Form.as_p()
559
618
560
- ``as_p()`` renders the form using the template assigned to the forms
561
- ``template_name_p`` attribute, by default this template is
562
- ``'django/forms/p.html'``. This template renders the form as a series of
563
- ``<p>`` tags, with each ``<p>`` containing one field::
619
+ ``as_p()`` renders the form as a series of ``<p>`` tags, with each ``<p>``
620
+ containing one field::
564
621
565
622
>>> f = ContactForm()
566
623
>>> f.as_p()
@@ -572,16 +629,17 @@ template, see also :ref:`overriding-built-in-form-templates`.
572
629
<p><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself"></p>
573
630
574
631
``as_ul()``
575
- -----------
632
+ ~~~~~~~~~~~
633
+
634
+ .. attribute:: Form.template_name_ul
635
+
636
+ The template used by ``as_ul()``. Default: ``'django/forms/ul.html'``.
576
637
577
638
.. method:: Form.as_ul()
578
639
579
- ``as_ul()`` renders the form using the template assigned to the forms
580
- ``template_name_ul`` attribute, by default this template is
581
- ``'django/forms/ul.html'``. This template renders the form as a series of
582
- ``<li>`` tags, with each ``<li>`` containing one field. It does *not* include
583
- the ``<ul>`` or ``</ul>``, so that you can specify any HTML attributes on the
584
- ``<ul>`` for flexibility::
640
+ ``as_ul()`` renders the form as a series of ``<li>`` tags, with each ``<li>``
641
+ containing one field. It does *not* include the ``<ul>`` or ``</ul>``, so that
642
+ you can specify any HTML attributes on the ``<ul>`` for flexibility::
585
643
586
644
>>> f = ContactForm()
587
645
>>> f.as_ul()
@@ -593,14 +651,15 @@ the ``<ul>`` or ``</ul>``, so that you can specify any HTML attributes on the
593
651
<li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself"></li>
594
652
595
653
``as_table()``
596
- --------------
654
+ ~~~~~~~~~~~~~~
655
+
656
+ .. attribute:: Form.template_name_table
657
+
658
+ The template used by ``as_table()``. Default: ``'django/forms/table.html'``.
597
659
598
660
.. method:: Form.as_table()
599
661
600
- Finally, ``as_table()`` renders the form using the template assigned to the
601
- forms ``template_name_table`` attribute, by default this template is
602
- ``'django/forms/table.html'``. This template outputs the form as an HTML
603
- ``<table>``::
662
+ ``as_table()`` renders the form as an HTML ``<table>``::
604
663
605
664
>>> f = ContactForm()
606
665
>>> f.as_table()
@@ -611,37 +670,6 @@ forms ``template_name_table`` attribute, by default this template is
611
670
<tr><th><label for="id_sender">Sender:</label></th><td><input type="email" name="sender" id="id_sender" required></td></tr>
612
671
<tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself"></td></tr>
613
672
614
- ``get_context()``
615
- -----------------
616
-
617
- .. versionadded:: 4.0
618
-
619
- .. method:: Form.get_context()
620
-
621
- Return context for form rendering in a template.
622
-
623
- The available context is:
624
-
625
- * ``form``: The bound form.
626
- * ``fields``: All bound fields, except the hidden fields.
627
- * ``hidden_fields``: All hidden bound fields.
628
- * ``errors``: All non field related or hidden field related form errors.
629
-
630
- ``render()``
631
- ------------
632
-
633
- .. versionadded:: 4.0
634
-
635
- .. method:: Form.render(template_name=None, context=None, renderer=None)
636
-
637
- The render method is called by ``__str__`` as well as the
638
- :meth:`.Form.as_table`, :meth:`.Form.as_p`, and :meth:`.Form.as_ul` methods.
639
- All arguments are optional and default to:
640
-
641
- * ``template_name``: :attr:`.Form.template_name`
642
- * ``context``: Value returned by :meth:`.Form.get_context`
643
- * ``renderer``: Value returned by :attr:`.Form.default_renderer`
644
-
645
673
.. _ref-forms-api-styling-form-rows:
646
674
647
675
Styling required or erroneous form rows
0 commit comments