panels.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. .. _editing-api:
  2. Panel types
  3. ===========
  4. Built-in Fields and Choosers
  5. ----------------------------
  6. Django's field types are automatically recognised and provided with an appropriate widget for input. Just define that field the normal Django way and pass the field name into :class:`~wagtail.admin.edit_handlers.FieldPanel` when defining your panels. Wagtail will take care of the rest.
  7. Here are some Wagtail-specific types that you might include as fields in your models.
  8. .. module:: wagtail.admin.edit_handlers
  9. FieldPanel
  10. ~~~~~~~~~~
  11. .. class:: FieldPanel(field_name, classname=None, widget=None, heading='', disable_comments=False)
  12. This is the panel used for basic Django field types.
  13. .. attribute:: FieldPanel.field_name
  14. This is the name of the class property used in your model definition.
  15. .. attribute:: FieldPanel.classname
  16. This is a string of optional CSS classes given to the panel which are used in formatting and scripted interactivity. By default, panels are formatted as inset fields.
  17. The CSS class ``full`` can be used to format the panel so it covers the full width of the Wagtail page editor.
  18. The CSS class ``title`` can be used to give the field a larger text size, suitable for representing page titles and section headings.
  19. .. attribute:: FieldPanel.widget (optional)
  20. This parameter allows you to specify a :doc:`Django form widget <django:ref/forms/widgets>` to use instead of the default widget for this field type.
  21. .. attribute:: FieldPanel.heading (optional)
  22. This allows you to override the heading for the panel, which will otherwise be set automatically using the form field's label (taken in turn from a model field's ``verbose_name``).
  23. .. attribute:: FieldPanel.disable_comments (optional)
  24. This allows you to prevent a field level comment button showing for this panel if set to ``True`` (see :ref:`commenting`).
  25. StreamFieldPanel
  26. ~~~~~~~~~~~~~~~~
  27. .. class:: StreamFieldPanel(field_name, classname=None, widget=None)
  28. Deprecated; use ``FieldPanel`` instead.
  29. .. versionchanged:: 2.17
  30. ``StreamFieldPanel`` is no longer required for ``StreamField``.
  31. MultiFieldPanel
  32. ~~~~~~~~~~~~~~~
  33. .. class:: MultiFieldPanel(children, heading="", classname=None)
  34. This panel condenses several :class:`~wagtail.admin.edit_handlers.FieldPanel` s or choosers, from a ``list`` or ``tuple``, under a single ``heading`` string.
  35. .. attribute:: MultiFieldPanel.children
  36. A ``list`` or ``tuple`` of child panels
  37. .. attribute:: MultiFieldPanel.heading
  38. A heading for the fields
  39. InlinePanel
  40. ~~~~~~~~~~~
  41. .. class:: InlinePanel(relation_name, panels=None, classname='', heading='', label='', help_text='', min_num=None, max_num=None)
  42. This panel allows for the creation of a "cluster" of related objects over a join to a separate model, such as a list of related links or slides to an image carousel.
  43. This is a powerful but complex feature which will take some space to cover, so we'll skip over it for now. For a full explanation on the usage of ``InlinePanel``, see :ref:`inline_panels`.
  44. .. topic:: Collapsing InlinePanels to save space
  45. Note that you can use ``classname="collapsible collapsed"`` to load the panel collapsed under its heading in order to save space in the Wagtail admin.
  46. See :ref:`collapsible` for more details on ``collapsible`` usage.
  47. FieldRowPanel
  48. ~~~~~~~~~~~~~
  49. .. class:: FieldRowPanel(children, classname=None)
  50. This panel creates a columnar layout in the editing interface, where each of the child Panels appears alongside each other rather than below.
  51. Use of FieldRowPanel particularly helps reduce the "snow-blindness" effect of seeing so many fields on the page, for complex models. It also improves the perceived association between fields of a similar nature. For example if you created a model representing an "Event" which had a starting date and ending date, it may be intuitive to find the start and end date on the same "row".
  52. By default, the panel is divided into equal-width columns, but this can be overridden by adding ``col*`` class names to each of the child Panels of the FieldRowPanel. The Wagtail editing interface is laid out using a grid system, in which the maximum width of the editor is 12 columns. Classes ``col1``-``col12`` can be applied to each child of a FieldRowPanel. The class ``col3`` will ensure that field appears 3 columns wide or a quarter the width. ``col4`` would cause the field to be 4 columns wide, or a third the width.
  53. .. attribute:: FieldRowPanel.children
  54. A ``list`` or ``tuple`` of child panels to display on the row
  55. .. attribute:: FieldRowPanel.classname
  56. A class to apply to the FieldRowPanel as a whole
  57. HelpPanel
  58. ~~~~~~~~~
  59. .. class:: HelpPanel(content='', template='wagtailadmin/edit_handlers/help_panel.html', heading='', classname='')
  60. .. attribute:: HelpPanel.content
  61. HTML string that gets displayed in the panel.
  62. .. attribute:: HelpPanel.template
  63. Path to a template rendering the full panel HTML.
  64. .. attribute:: HelpPanel.heading
  65. A heading for the help content.
  66. .. attribute:: HelpPanel.classname
  67. String of CSS classes given to the panel which are used in formatting and scripted interactivity.
  68. PageChooserPanel
  69. ~~~~~~~~~~~~~~~~
  70. .. class:: PageChooserPanel(field_name, page_type=None, can_choose_root=False)
  71. You can explicitly link :class:`~wagtail.core.models.Page`-derived models together using the :class:`~wagtail.core.models.Page` model and ``PageChooserPanel``.
  72. .. code-block:: python
  73. from wagtail.core.models import Page
  74. from wagtail.admin.edit_handlers import PageChooserPanel
  75. class BookPage(Page):
  76. related_page = models.ForeignKey(
  77. 'wagtailcore.Page',
  78. null=True,
  79. blank=True,
  80. on_delete=models.SET_NULL,
  81. related_name='+',
  82. )
  83. content_panels = Page.content_panels + [
  84. PageChooserPanel('related_page', 'demo.PublisherPage'),
  85. ]
  86. ``PageChooserPanel`` takes one required argument, the field name. Optionally, specifying a page type (in the form of an ``"appname.modelname"`` string) will filter the chooser to display only pages of that type. A list or tuple of page types can also be passed in, to allow choosing a page that matches any of those page types:
  87. .. code-block:: python
  88. PageChooserPanel('related_page', ['demo.PublisherPage', 'demo.AuthorPage'])
  89. Passing ``can_choose_root=True`` will allow the editor to choose the tree root as a page. Normally this would be undesirable, since the tree root is never a usable page, but in some specialised cases it may be appropriate; for example, a page with an automatic "related articles" feed could use a PageChooserPanel to select which subsection articles will be taken from, with the root corresponding to 'everywhere'.
  90. .. versionchanged:: 2.17
  91. ``FieldPanel`` now also provides a page chooser interface for foreign keys to page models. ``PageChooserPanel`` is only required when specifying the ``page_type`` or ``can_choose_root`` parameters.
  92. ImageChooserPanel
  93. ~~~~~~~~~~~~~~~~~
  94. .. module:: wagtail.images.edit_handlers
  95. .. class:: ImageChooserPanel(field_name)
  96. Deprecated; use ``FieldPanel`` instead.
  97. .. versionchanged:: 2.17
  98. ``ImageChooserPanel`` is no longer required to obtain an image chooser interface.
  99. FormSubmissionsPanel
  100. ~~~~~~~~~~~~~~~~~~~~
  101. .. module:: wagtail.contrib.forms.edit_handlers
  102. .. class:: FormSubmissionsPanel
  103. This panel adds a single, read-only section in the edit interface for pages implementing the :class:`~wagtail.contrib.forms.models.AbstractForm` model.
  104. It includes the number of total submissions for the given form and also a link to the listing of submissions.
  105. .. code-block:: python
  106. from wagtail.contrib.forms.models import AbstractForm
  107. from wagtail.contrib.forms.edit_handlers import FormSubmissionsPanel
  108. class ContactFormPage(AbstractForm):
  109. content_panels = [
  110. FormSubmissionsPanel(),
  111. ]
  112. DocumentChooserPanel
  113. ~~~~~~~~~~~~~~~~~~~~
  114. .. module:: wagtail.documents.edit_handlers
  115. .. class:: DocumentChooserPanel(field_name)
  116. Deprecated; use ``FieldPanel`` instead.
  117. .. versionchanged:: 2.17
  118. ``DocumentChooserPanel`` is no longer required to obtain a document chooser interface.
  119. SnippetChooserPanel
  120. ~~~~~~~~~~~~~~~~~~~
  121. .. module:: wagtail.snippets.edit_handlers
  122. .. class:: SnippetChooserPanel(field_name, snippet_type=None)
  123. Deprecated; use ``FieldPanel`` instead.
  124. .. versionchanged:: 2.17
  125. ``SnippetChooserPanel`` is no longer required to obtain a document chooser interface.
  126. Field Customisation
  127. -------------------
  128. By adding CSS classes to your panel definitions or adding extra parameters to your field definitions, you can control much of how your fields will display in the Wagtail page editing interface. Wagtail's page editing interface takes much of its behaviour from Django's admin, so you may find many options for customisation covered there. (See :doc:`Django model field reference <ref/models/fields>`).
  129. Full-Width Input
  130. ~~~~~~~~~~~~~~~~
  131. Use ``classname="full"`` to make a field (input element) stretch the full width of the Wagtail page editor. This will not work if the field is encapsulated in a :class:`~wagtail.admin.edit_handlers.MultiFieldPanel`, which places its child fields into a formset.
  132. Titles
  133. ~~~~~~
  134. Use ``classname="title"`` to make Page's built-in title field stand out with more vertical padding.
  135. .. _collapsible:
  136. Collapsible
  137. ~~~~~~~~~~~
  138. By default, panels are expanded and not collapsible.
  139. Use ``classname="collapsible"`` to enable the collapse control.
  140. Use ``classname="collapsible collapsed"`` will load the editor page with the panel collapsed under its heading.
  141. You must define a ``heading`` when using ``collapsible`` with ``MultiFieldPanel``.
  142. You must define a ``heading`` or ``label`` when using ``collapsible`` with ``InlinePanel``.
  143. .. code-block:: python
  144. content_panels = [
  145. MultiFieldPanel(
  146. [
  147. ImageChooserPanel('cover'),
  148. DocumentChooserPanel('book_file'),
  149. PageChooserPanel('publisher'),
  150. ],
  151. heading="Collection of Book Fields",
  152. classname="collapsible collapsed"
  153. ),
  154. ]
  155. Placeholder Text
  156. ~~~~~~~~~~~~~~~~
  157. By default, Wagtail uses the field's label as placeholder text. To change it, pass to the FieldPanel a widget with a placeholder attribute set to your desired text. You can select widgets from :doc:`Django's form widgets <django:ref/forms/widgets>`, or any of the Wagtail's widgets found in ``wagtail.admin.widgets``.
  158. For example, to customize placeholders for a Book model exposed via ModelAdmin:
  159. .. code-block:: python
  160. # models.py
  161. from django import forms # the default Django widgets live here
  162. from wagtail.admin import widgets # to use Wagtail's special datetime widget
  163. class Book(models.Model):
  164. title = models.CharField(max_length=256)
  165. release_date = models.DateField()
  166. price = models.DecimalField(max_digits=5, decimal_places=2)
  167. # you can create them separately
  168. title_widget = forms.TextInput(
  169. attrs = {
  170. 'placeholder': 'Enter Full Title'
  171. }
  172. )
  173. # using the correct widget for your field type and desired effect
  174. date_widget = widgets.AdminDateInput(
  175. attrs = {
  176. 'placeholder': 'dd-mm-yyyy'
  177. }
  178. )
  179. panels = [
  180. FieldPanel('title', widget=title_widget), # then add them as a variable
  181. FieldPanel('release_date', widget=date_widget),
  182. FieldPanel('price', widget=forms.NumberInput(attrs={'placeholder': 'Retail price on release'})) # or directly inline
  183. ]
  184. Required Fields
  185. ~~~~~~~~~~~~~~~
  186. To make input or chooser selection mandatory for a field, add :attr:`blank=False <django.db.models.Field.blank>` to its model definition.
  187. Hiding Fields
  188. ~~~~~~~~~~~~~
  189. Without a panel definition, a default form field (without label) will be used to represent your fields. If you intend to hide a field on the Wagtail page editor, define the field with :attr:`editable=False <django.db.models.Field.editable>`.
  190. .. _inline_panels:
  191. Inline Panels and Model Clusters
  192. --------------------------------
  193. The ``django-modelcluster`` module allows for streamlined relation of extra models to a Wagtail page via a ForeignKey-like relationship called ``ParentalKey``. Normally, your related objects "cluster" would need to be created beforehand (or asynchronously) before being linked to a Page; however, objects related to a Wagtail page via ``ParentalKey`` can be created on-the-fly and saved to a draft revision of a ``Page`` object.
  194. Let's look at the example of adding related links to a :class:`~wagtail.core.models.Page`-derived model. We want to be able to add as many as we like, assign an order, and do all of this without leaving the page editing screen.
  195. .. code-block:: python
  196. from wagtail.core.models import Orderable, Page
  197. from modelcluster.fields import ParentalKey
  198. # The abstract model for related links, complete with panels
  199. class RelatedLink(models.Model):
  200. title = models.CharField(max_length=255)
  201. link_external = models.URLField("External link", blank=True)
  202. panels = [
  203. FieldPanel('title'),
  204. FieldPanel('link_external'),
  205. ]
  206. class Meta:
  207. abstract = True
  208. # The real model which combines the abstract model, an
  209. # Orderable helper class, and what amounts to a ForeignKey link
  210. # to the model we want to add related links to (BookPage)
  211. class BookPageRelatedLinks(Orderable, RelatedLink):
  212. page = ParentalKey('demo.BookPage', on_delete=models.CASCADE, related_name='related_links')
  213. class BookPage(Page):
  214. # ...
  215. content_panels = Page.content_panels + [
  216. InlinePanel('related_links', label="Related Links"),
  217. ]
  218. The ``RelatedLink`` class is a vanilla Django abstract model. The ``BookPageRelatedLinks`` model extends it with capability for being ordered in the Wagtail interface via the ``Orderable`` class as well as adding a ``page`` property which links the model to the ``BookPage`` model we're adding the related links objects to. Finally, in the panel definitions for ``BookPage``, we'll add an :class:`~wagtail.admin.edit_handlers.InlinePanel` to provide an interface for it all. Let's look again at the parameters that :class:`~wagtail.admin.edit_handlers.InlinePanel` accepts:
  219. .. code-block:: python
  220. InlinePanel( relation_name, panels=None, heading='', label='', help_text='', min_num=None, max_num=None )
  221. The ``relation_name`` is the ``related_name`` label given to the cluster's ``ParentalKey`` relation. You can add the ``panels`` manually or make them part of the cluster model. ``heading`` and ``help_text`` provide a heading and caption, respectively, for the Wagtail editor. ``label`` sets the text on the add button, and is used as the heading when ``heading`` is not present. Finally, ``min_num`` and ``max_num`` allow you to set the minimum/maximum number of forms that the user must submit.
  222. For another example of using model clusters, see :ref:`tagging`
  223. For more on ``django-modelcluster``, visit `the django-modelcluster github project page`_.
  224. .. _the django-modelcluster github project page: https://github.com/torchbox/django-modelcluster