panels.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. This is the panel used for Wagtail's StreamField type (see :ref:`streamfield`).
  29. .. attribute:: FieldPanel.field_name
  30. This is the name of the class property used in your model definition.
  31. .. attribute:: FieldPanel.classname (optional)
  32. 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.
  33. The CSS class ``full`` can be used to format the panel so it covers the full width of the Wagtail page editor.
  34. MultiFieldPanel
  35. ~~~~~~~~~~~~~~~
  36. .. class:: MultiFieldPanel(children, heading="", classname=None)
  37. This panel condenses several :class:`~wagtail.admin.edit_handlers.FieldPanel` s or choosers, from a ``list`` or ``tuple``, under a single ``heading`` string.
  38. .. attribute:: MultiFieldPanel.children
  39. A ``list`` or ``tuple`` of child panels
  40. .. attribute:: MultiFieldPanel.heading
  41. A heading for the fields
  42. InlinePanel
  43. ~~~~~~~~~~~
  44. .. class:: InlinePanel(relation_name, panels=None, classname='', heading='', label='', help_text='', min_num=None, max_num=None)
  45. 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.
  46. 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`.
  47. .. topic:: Collapsing InlinePanels to save space
  48. 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.
  49. See :ref:`collapsible` for more details on ``collapsible`` usage.
  50. FieldRowPanel
  51. ~~~~~~~~~~~~~
  52. .. class:: FieldRowPanel(children, classname=None)
  53. This panel creates a columnar layout in the editing interface, where each of the child Panels appears alongside each other rather than below.
  54. 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".
  55. 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.
  56. .. attribute:: FieldRowPanel.children
  57. A ``list`` or ``tuple`` of child panels to display on the row
  58. .. attribute:: FieldRowPanel.classname
  59. A class to apply to the FieldRowPanel as a whole
  60. HelpPanel
  61. ~~~~~~~~~
  62. .. class:: HelpPanel(content='', template='wagtailadmin/edit_handlers/help_panel.html', heading='', classname='')
  63. .. attribute:: HelpPanel.content
  64. HTML string that gets displayed in the panel.
  65. .. attribute:: HelpPanel.template
  66. Path to a template rendering the full panel HTML.
  67. .. attribute:: HelpPanel.heading
  68. A heading for the help content.
  69. .. attribute:: HelpPanel.classname
  70. String of CSS classes given to the panel which are used in formatting and scripted interactivity.
  71. PageChooserPanel
  72. ~~~~~~~~~~~~~~~~
  73. .. class:: PageChooserPanel(field_name, page_type=None, can_choose_root=False)
  74. You can explicitly link :class:`~wagtail.core.models.Page`-derived models together using the :class:`~wagtail.core.models.Page` model and ``PageChooserPanel``.
  75. .. code-block:: python
  76. from wagtail.core.models import Page
  77. from wagtail.admin.edit_handlers import PageChooserPanel
  78. class BookPage(Page):
  79. related_page = models.ForeignKey(
  80. 'wagtailcore.Page',
  81. null=True,
  82. blank=True,
  83. on_delete=models.SET_NULL,
  84. related_name='+',
  85. )
  86. content_panels = Page.content_panels + [
  87. PageChooserPanel('related_page', 'demo.PublisherPage'),
  88. ]
  89. ``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:
  90. .. code-block:: python
  91. PageChooserPanel('related_page', ['demo.PublisherPage', 'demo.AuthorPage'])
  92. 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'.
  93. ImageChooserPanel
  94. ~~~~~~~~~~~~~~~~~
  95. .. module:: wagtail.images.edit_handlers
  96. .. class:: ImageChooserPanel(field_name)
  97. Wagtail includes a unified image library, which you can access in your models through the :class:`~wagtail.images.models.Image` model and the ``ImageChooserPanel`` chooser. Here's how:
  98. .. code-block:: python
  99. from wagtail.images.models import Image
  100. from wagtail.images.edit_handlers import ImageChooserPanel
  101. class BookPage(Page):
  102. cover = models.ForeignKey(
  103. 'wagtailimages.Image',
  104. null=True,
  105. blank=True,
  106. on_delete=models.SET_NULL,
  107. related_name='+'
  108. )
  109. content_panels = Page.content_panels + [
  110. ImageChooserPanel('cover'),
  111. ]
  112. Django's default behaviour is to "cascade" deletions through a ForeignKey relationship, which may not be what you want. This is why the :attr:`~django.db.models.Field.null`, :attr:`~django.db.models.Field.blank`, and :attr:`~django.db.models.ForeignKey.on_delete` parameters should be set to allow for an empty field. ``ImageChooserPanel`` takes only one argument: the name of the field.
  113. Displaying ``Image`` objects in a template requires the use of a template tag. See :ref:`image_tag`.
  114. FormSubmissionsPanel
  115. ~~~~~~~~~~~~~~~~~~~~
  116. .. module:: wagtail.contrib.forms.edit_handlers
  117. .. class:: FormSubmissionsPanel
  118. This panel adds a single, read-only section in the edit interface for pages implementing the :class:`~wagtail.contrib.forms.models.AbstractForm` model.
  119. It includes the number of total submissions for the given form and also a link to the listing of submissions.
  120. .. code-block:: python
  121. from wagtail.contrib.forms.models import AbstractForm
  122. from wagtail.contrib.forms.edit_handlers import FormSubmissionsPanel
  123. class ContactFormPage(AbstractForm):
  124. content_panels = [
  125. FormSubmissionsPanel(),
  126. ]
  127. DocumentChooserPanel
  128. ~~~~~~~~~~~~~~~~~~~~
  129. .. module:: wagtail.documents.edit_handlers
  130. .. class:: DocumentChooserPanel(field_name)
  131. For files in other formats, Wagtail provides a generic file store through the :class:`~wagtail.documents.models.Document` model:
  132. .. code-block:: python
  133. from wagtail.documents.models import Document
  134. from wagtail.documents.edit_handlers import DocumentChooserPanel
  135. class BookPage(Page):
  136. book_file = models.ForeignKey(
  137. 'wagtaildocs.Document',
  138. null=True,
  139. blank=True,
  140. on_delete=models.SET_NULL,
  141. related_name='+'
  142. )
  143. content_panels = Page.content_panels + [
  144. DocumentChooserPanel('book_file'),
  145. ]
  146. As with images, Wagtail documents should also have the appropriate extra parameters to prevent cascade deletions across a ForeignKey relationship. ``DocumentChooserPanel`` takes only one argument: the name of the field.
  147. SnippetChooserPanel
  148. ~~~~~~~~~~~~~~~~~~~
  149. .. module:: wagtail.snippets.edit_handlers
  150. .. class:: SnippetChooserPanel(field_name, snippet_type=None)
  151. Snippets are vanilla Django models you create yourself without a Wagtail-provided base class. A chooser, ``SnippetChooserPanel``, is provided which takes the field name as an argument.
  152. .. code-block:: python
  153. from wagtail.snippets.edit_handlers import SnippetChooserPanel
  154. class BookPage(Page):
  155. advert = models.ForeignKey(
  156. 'demo.Advert',
  157. null=True,
  158. blank=True,
  159. on_delete=models.SET_NULL,
  160. related_name='+'
  161. )
  162. content_panels = Page.content_panels + [
  163. SnippetChooserPanel('advert'),
  164. ]
  165. See :ref:`snippets` for more information.
  166. Field Customisation
  167. -------------------
  168. 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>`).
  169. Full-Width Input
  170. ~~~~~~~~~~~~~~~~
  171. 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.
  172. Titles
  173. ~~~~~~
  174. Use ``classname="title"`` to make Page's built-in title field stand out with more vertical padding.
  175. .. _collapsible:
  176. Collapsible
  177. ~~~~~~~~~~~
  178. By default, panels are expanded and not collapsible.
  179. Use ``classname="collapsible"`` to enable the collapse control.
  180. Use ``classname="collapsible collapsed"`` will load the editor page with the panel collapsed under its heading.
  181. You must define a ``heading`` when using ``collapsible`` with ``MultiFieldPanel``.
  182. You must define a ``heading`` or ``label`` when using ``collapsible`` with ``InlinePanel``.
  183. .. code-block:: python
  184. content_panels = [
  185. MultiFieldPanel(
  186. [
  187. ImageChooserPanel('cover'),
  188. DocumentChooserPanel('book_file'),
  189. PageChooserPanel('publisher'),
  190. ],
  191. heading="Collection of Book Fields",
  192. classname="collapsible collapsed"
  193. ),
  194. ]
  195. Placeholder Text
  196. ~~~~~~~~~~~~~~~~
  197. 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``.
  198. For example, to customize placeholders for a Book model exposed via ModelAdmin:
  199. .. code-block:: python
  200. # models.py
  201. from django import forms # the default Django widgets live here
  202. from wagtail.admin import widgets # to use Wagtail's special datetime widget
  203. class Book(models.Model):
  204. title = models.CharField(max_length=256)
  205. release_date = models.DateField()
  206. price = models.DecimalField(max_digits=5, decimal_places=2)
  207. # you can create them separately
  208. title_widget = forms.TextInput(
  209. attrs = {
  210. 'placeholder': 'Enter Full Title'
  211. }
  212. )
  213. # using the correct widget for your field type and desired effect
  214. date_widget = widgets.AdminDateInput(
  215. attrs = {
  216. 'placeholder': 'dd-mm-yyyy'
  217. }
  218. )
  219. panels = [
  220. FieldPanel('title', widget=title_widget), # then add them as a variable
  221. FieldPanel('release_date', widget=date_widget),
  222. FieldPanel('price', widget=forms.NumberInput(attrs={'placeholder': 'Retail price on release'})) # or directly inline
  223. ]
  224. Required Fields
  225. ~~~~~~~~~~~~~~~
  226. To make input or chooser selection mandatory for a field, add :attr:`blank=False <django.db.models.Field.blank>` to its model definition.
  227. Hiding Fields
  228. ~~~~~~~~~~~~~
  229. 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>`.
  230. .. _inline_panels:
  231. Inline Panels and Model Clusters
  232. --------------------------------
  233. 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.
  234. 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.
  235. .. code-block:: python
  236. from wagtail.core.models import Orderable, Page
  237. from modelcluster.fields import ParentalKey
  238. # The abstract model for related links, complete with panels
  239. class RelatedLink(models.Model):
  240. title = models.CharField(max_length=255)
  241. link_external = models.URLField("External link", blank=True)
  242. panels = [
  243. FieldPanel('title'),
  244. FieldPanel('link_external'),
  245. ]
  246. class Meta:
  247. abstract = True
  248. # The real model which combines the abstract model, an
  249. # Orderable helper class, and what amounts to a ForeignKey link
  250. # to the model we want to add related links to (BookPage)
  251. class BookPageRelatedLinks(Orderable, RelatedLink):
  252. page = ParentalKey('demo.BookPage', on_delete=models.CASCADE, related_name='related_links')
  253. class BookPage(Page):
  254. # ...
  255. content_panels = Page.content_panels + [
  256. InlinePanel('related_links', label="Related Links"),
  257. ]
  258. 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:
  259. .. code-block:: python
  260. InlinePanel( relation_name, panels=None, heading='', label='', help_text='', min_num=None, max_num=None )
  261. 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.
  262. For another example of using model clusters, see :ref:`tagging`
  263. For more on ``django-modelcluster``, visit `the django-modelcluster github project page`_.
  264. .. _the django-modelcluster github project page: https://github.com/torchbox/django-modelcluster