panels.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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, permission=None)
  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. .. attribute:: FieldPanel.permission (optional)
  26. Allows a field to be selectively shown to users with sufficient permission. Accepts a permission codename such as ``'myapp.change_blog_category'`` - if the logged-in user does not have that permission, the field will be omitted from the form. See Django's documentation on :ref:`custom permissions <django:custom-permissions>` for details on how to set permissions up; alternatively, if you want to set a field as only available to superusers, you can use any arbitrary string (such as ``'superuser'``) as the codename, since superusers automatically pass all permission tests.
  27. StreamFieldPanel
  28. ~~~~~~~~~~~~~~~~
  29. .. class:: StreamFieldPanel(field_name, classname=None, widget=None)
  30. Deprecated; use ``FieldPanel`` instead.
  31. .. versionchanged:: 2.17
  32. ``StreamFieldPanel`` is no longer required for ``StreamField``.
  33. MultiFieldPanel
  34. ~~~~~~~~~~~~~~~
  35. .. class:: MultiFieldPanel(children, heading="", classname=None)
  36. This panel condenses several :class:`~wagtail.admin.edit_handlers.FieldPanel` s or choosers, from a ``list`` or ``tuple``, under a single ``heading`` string.
  37. .. attribute:: MultiFieldPanel.children
  38. A ``list`` or ``tuple`` of child panels
  39. .. attribute:: MultiFieldPanel.heading
  40. A heading for the fields
  41. InlinePanel
  42. ~~~~~~~~~~~
  43. .. class:: InlinePanel(relation_name, panels=None, classname='', heading='', label='', help_text='', min_num=None, max_num=None)
  44. 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.
  45. 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`.
  46. .. topic:: Collapsing InlinePanels to save space
  47. 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.
  48. See :ref:`collapsible` for more details on ``collapsible`` usage.
  49. FieldRowPanel
  50. ~~~~~~~~~~~~~
  51. .. class:: FieldRowPanel(children, classname=None)
  52. This panel creates a columnar layout in the editing interface, where each of the child Panels appears alongside each other rather than below.
  53. 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".
  54. 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.
  55. .. attribute:: FieldRowPanel.children
  56. A ``list`` or ``tuple`` of child panels to display on the row
  57. .. attribute:: FieldRowPanel.classname
  58. A class to apply to the FieldRowPanel as a whole
  59. HelpPanel
  60. ~~~~~~~~~
  61. .. class:: HelpPanel(content='', template='wagtailadmin/edit_handlers/help_panel.html', heading='', classname='')
  62. .. attribute:: HelpPanel.content
  63. HTML string that gets displayed in the panel.
  64. .. attribute:: HelpPanel.template
  65. Path to a template rendering the full panel HTML.
  66. .. attribute:: HelpPanel.heading
  67. A heading for the help content.
  68. .. attribute:: HelpPanel.classname
  69. String of CSS classes given to the panel which are used in formatting and scripted interactivity.
  70. PageChooserPanel
  71. ~~~~~~~~~~~~~~~~
  72. .. class:: PageChooserPanel(field_name, page_type=None, can_choose_root=False)
  73. You can explicitly link :class:`~wagtail.models.Page`-derived models together using the :class:`~wagtail.models.Page` model and ``PageChooserPanel``.
  74. .. code-block:: python
  75. from wagtail.models import Page
  76. from wagtail.admin.edit_handlers import PageChooserPanel
  77. class BookPage(Page):
  78. related_page = models.ForeignKey(
  79. 'wagtailcore.Page',
  80. null=True,
  81. blank=True,
  82. on_delete=models.SET_NULL,
  83. related_name='+',
  84. )
  85. content_panels = Page.content_panels + [
  86. PageChooserPanel('related_page', 'demo.PublisherPage'),
  87. ]
  88. ``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:
  89. .. code-block:: python
  90. PageChooserPanel('related_page', ['demo.PublisherPage', 'demo.AuthorPage'])
  91. 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'.
  92. .. versionchanged:: 2.17
  93. ``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.
  94. ImageChooserPanel
  95. ~~~~~~~~~~~~~~~~~
  96. .. module:: wagtail.images.edit_handlers
  97. .. class:: ImageChooserPanel(field_name)
  98. Deprecated; use ``FieldPanel`` instead.
  99. .. versionchanged:: 2.17
  100. ``ImageChooserPanel`` is no longer required to obtain an image chooser interface.
  101. FormSubmissionsPanel
  102. ~~~~~~~~~~~~~~~~~~~~
  103. .. module:: wagtail.contrib.forms.edit_handlers
  104. .. class:: FormSubmissionsPanel
  105. This panel adds a single, read-only section in the edit interface for pages implementing the :class:`~wagtail.contrib.forms.models.AbstractForm` model.
  106. It includes the number of total submissions for the given form and also a link to the listing of submissions.
  107. .. code-block:: python
  108. from wagtail.contrib.forms.models import AbstractForm
  109. from wagtail.contrib.forms.edit_handlers import FormSubmissionsPanel
  110. class ContactFormPage(AbstractForm):
  111. content_panels = [
  112. FormSubmissionsPanel(),
  113. ]
  114. DocumentChooserPanel
  115. ~~~~~~~~~~~~~~~~~~~~
  116. .. module:: wagtail.documents.edit_handlers
  117. .. class:: DocumentChooserPanel(field_name)
  118. Deprecated; use ``FieldPanel`` instead.
  119. .. versionchanged:: 2.17
  120. ``DocumentChooserPanel`` is no longer required to obtain a document chooser interface.
  121. SnippetChooserPanel
  122. ~~~~~~~~~~~~~~~~~~~
  123. .. module:: wagtail.snippets.edit_handlers
  124. .. class:: SnippetChooserPanel(field_name, snippet_type=None)
  125. Deprecated; use ``FieldPanel`` instead.
  126. .. versionchanged:: 2.17
  127. ``SnippetChooserPanel`` is no longer required to obtain a document chooser interface.
  128. Field Customisation
  129. -------------------
  130. 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>`).
  131. Full-Width Input
  132. ~~~~~~~~~~~~~~~~
  133. 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.
  134. Titles
  135. ~~~~~~
  136. Use ``classname="title"`` to make Page's built-in title field stand out with more vertical padding.
  137. .. _collapsible:
  138. Collapsible
  139. ~~~~~~~~~~~
  140. By default, panels are expanded and not collapsible.
  141. Use ``classname="collapsible"`` to enable the collapse control.
  142. Use ``classname="collapsible collapsed"`` will load the editor page with the panel collapsed under its heading.
  143. You must define a ``heading`` when using ``collapsible`` with ``MultiFieldPanel``.
  144. You must define a ``heading`` or ``label`` when using ``collapsible`` with ``InlinePanel``.
  145. .. code-block:: python
  146. content_panels = [
  147. MultiFieldPanel(
  148. [
  149. ImageChooserPanel('cover'),
  150. DocumentChooserPanel('book_file'),
  151. PageChooserPanel('publisher'),
  152. ],
  153. heading="Collection of Book Fields",
  154. classname="collapsible collapsed"
  155. ),
  156. ]
  157. Placeholder Text
  158. ~~~~~~~~~~~~~~~~
  159. 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``.
  160. For example, to customize placeholders for a Book model exposed via ModelAdmin:
  161. .. code-block:: python
  162. # models.py
  163. from django import forms # the default Django widgets live here
  164. from wagtail.admin import widgets # to use Wagtail's special datetime widget
  165. class Book(models.Model):
  166. title = models.CharField(max_length=256)
  167. release_date = models.DateField()
  168. price = models.DecimalField(max_digits=5, decimal_places=2)
  169. # you can create them separately
  170. title_widget = forms.TextInput(
  171. attrs = {
  172. 'placeholder': 'Enter Full Title'
  173. }
  174. )
  175. # using the correct widget for your field type and desired effect
  176. date_widget = widgets.AdminDateInput(
  177. attrs = {
  178. 'placeholder': 'dd-mm-yyyy'
  179. }
  180. )
  181. panels = [
  182. FieldPanel('title', widget=title_widget), # then add them as a variable
  183. FieldPanel('release_date', widget=date_widget),
  184. FieldPanel('price', widget=forms.NumberInput(attrs={'placeholder': 'Retail price on release'})) # or directly inline
  185. ]
  186. Required Fields
  187. ~~~~~~~~~~~~~~~
  188. To make input or chooser selection mandatory for a field, add :attr:`blank=False <django.db.models.Field.blank>` to its model definition.
  189. Hiding Fields
  190. ~~~~~~~~~~~~~
  191. 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>`.
  192. .. _inline_panels:
  193. Inline Panels and Model Clusters
  194. --------------------------------
  195. 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.
  196. Let's look at the example of adding related links to a :class:`~wagtail.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.
  197. .. code-block:: python
  198. from wagtail.models import Orderable, Page
  199. from modelcluster.fields import ParentalKey
  200. # The abstract model for related links, complete with panels
  201. class RelatedLink(models.Model):
  202. title = models.CharField(max_length=255)
  203. link_external = models.URLField("External link", blank=True)
  204. panels = [
  205. FieldPanel('title'),
  206. FieldPanel('link_external'),
  207. ]
  208. class Meta:
  209. abstract = True
  210. # The real model which combines the abstract model, an
  211. # Orderable helper class, and what amounts to a ForeignKey link
  212. # to the model we want to add related links to (BookPage)
  213. class BookPageRelatedLinks(Orderable, RelatedLink):
  214. page = ParentalKey('demo.BookPage', on_delete=models.CASCADE, related_name='related_links')
  215. class BookPage(Page):
  216. # ...
  217. content_panels = Page.content_panels + [
  218. InlinePanel('related_links', label="Related Links"),
  219. ]
  220. 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:
  221. .. code-block:: python
  222. InlinePanel( relation_name, panels=None, heading='', label='', help_text='', min_num=None, max_num=None )
  223. 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.
  224. For another example of using model clusters, see :ref:`tagging`
  225. For more on ``django-modelcluster``, visit `the django-modelcluster github project page`_.
  226. .. _the django-modelcluster github project page: https://github.com/torchbox/django-modelcluster