related_pages.rst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. Related Pages
  2. =============
  3. .. versionadded:: 2.1
  4. Added related pages system. You must be on Wagtail CRX version 2.1 or higher in order to follow this guide.
  5. 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.
  6. .. figure:: img/related_pages.png
  7. :alt: Related pages showing similarly classified articles.
  8. Related pages showing similarly classified articles.
  9. Related page formatting
  10. ------------------------
  11. Each related page is rendered using the page model's "miniview" template.
  12. 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/dev/coderedcms/templates/coderedcms/pages/pages.mini.html>`_.
  13. 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()``.
  14. Related page customization
  15. --------------------------
  16. 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.
  17. 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.
  18. .. code-block:: python
  19. class ProductPage(CoderedPage):
  20. # Custom template that will be used when a Product
  21. # is shown on an Article page (below).
  22. miniview_template = "website/pages/product.mini.html"
  23. class ArticlePage(CoderedPage):
  24. # Show related pages by default when creating new Articles.
  25. related_show_default = True
  26. # By default, related sibling Articles will be shown.
  27. # Show related Products instead.
  28. related_query_pagemodel = "website.ProductPage"
  29. 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.
  30. .. code-block:: python
  31. class ProductPage(CoderedPage):
  32. price = models.DecimalField(max_digits=9, decimal_places=2)
  33. class ArticlePage(CoderedPage):
  34. # Show related pages by default when creating new Articles.
  35. related_show_default = True
  36. def get_related_pages(self) -> models.QuerySet:
  37. """Show most expensive products first."""
  38. return ProductPage.objects.live().order_by("-price")