related_pages.rst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. Related Pages
  2. =============
  3. Using the power of :doc:`/features/snippets/classifiers`, pages can automatically show a list of similarly classified pages. By default, this is enabled on :doc:`/features/page_types/article_pages`, but can be enabled on any page via the Wagtail Admin, or a 1-line code change on the page model.
  4. .. figure:: img/related_pages.png
  5. :alt: Related pages showing similarly classified articles.
  6. Related pages showing similarly classified articles.
  7. Related page formatting
  8. ------------------------
  9. Each related page is rendered using the page model's "miniview" template.
  10. The template can be overridden per model with the ``miniview_template`` attribute, the default of which is `coderedcms/pages/page.mini.html <https://github.com/coderedcorp/coderedcms/blob/main/coderedcms/templates/coderedcms/pages/pages.mini.html>`_.
  11. If related pages are enabled, a ``QuerySet`` of pages is added to the context as ``related_pages``. This ``QuerySet`` can also be retrieved by calling ``page.get_related_pages()``.
  12. Related page customization
  13. --------------------------
  14. Each page can have related pages turned on or off via a toggle in the page editor, under the **Layout** tab. Pages based on ``CoderedArticlePage`` have this setting enabled by default when new pages are created. To toggle the default value when creating new pages, set ``related_show_default`` to ``True`` or ``False``. To retroactively toggle this setting on existing pages, set the related ``related_show`` field using a manual query or migration.
  15. By default, sibling pages are queried and ordered based on the number of Classifier Terms in common. If you wish to query a different model --- for example to have Article pages show related Product pages instead --- set the ``related_query_pagemodel`` attribute to the desired content type.
  16. .. code-block:: python
  17. class ProductPage(CoderedPage):
  18. # Custom template that will be used when a Product
  19. # is shown on an Article page (below).
  20. miniview_template = "website/pages/product.mini.html"
  21. class ArticlePage(CoderedPage):
  22. # Show related pages by default when creating new Articles.
  23. related_show_default = True
  24. # By default, related sibling Articles will be shown.
  25. # Show related Products instead.
  26. related_query_pagemodel = "website.ProductPage"
  27. If you'd instead prefer to totally customize the related algorithm, override the ``get_related_pages()`` function. Just be sure to return a ``QuerySet`` of pages.
  28. .. code-block:: python
  29. class ProductPage(CoderedPage):
  30. price = models.DecimalField(max_digits=9, decimal_places=2)
  31. class ArticlePage(CoderedPage):
  32. # Show related pages by default when creating new Articles.
  33. related_show_default = True
  34. def get_related_pages(self) -> models.QuerySet:
  35. """Show most expensive products first."""
  36. return ProductPage.objects.live().order_by("-price")
  37. .. versionadded:: 2.1
  38. You must be on Wagtail CRX version 2.1 or higher to use related pages.