2
0

classifiers.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. Classifiers
  2. ===========
  3. Classifiers provide a way to create custom categories or groups to organize and filter pages.
  4. Usage
  5. -----
  6. Let's say you want to create a custom category: "Blog Category". Blog Category will be used
  7. to filter article/blog pages on the site.
  8. First, create a new Classifier under **Snippets > Classifiers** called "Blog Category"
  9. and add several Terms underneath the Classifier, for example: "News", "Opinion", and "Press Releases".
  10. These terms will function as categories. Create and reorder these Terms as needed.
  11. Save the Classifier when finished.
  12. Second, classify various Article pages by Blog Category terms:
  13. * Edit an Article page.
  14. * Open the **Classify** tab, and select the appropriate terms.
  15. * Publish the page when finished.
  16. To enable filtering Article pages by "Blog Category":
  17. * Edit your Article Landing Page (may be named differently on your project - it should be the
  18. parent page of your Article Pages).
  19. * Open the **Layout** tab, enable **Show child pages**, and then select "Blog Category"
  20. under **Filter child pages by** .
  21. * Publish or preview the page, and you'll now see filtering options for every term under
  22. Blog Category.
  23. Going a bit further, let's show a preview of the top 3 newest blog pages classified as "News"
  24. automatically on the home page:
  25. * Edit the home page.
  26. * In the **Content** tab anywhere in the **Body** add a Responsive Grid Row, and then add a
  27. **Latest Pages** block.
  28. * Set the **Parent page** to your Article landing page, and **Classified as** to
  29. "Blog Category > News".
  30. * Publish or preview the page, and you'll now see the latest 3 articles classified as "News"
  31. on the home page.
  32. Classifiers are not just limited to Article pages, they work an every page on the site.
  33. Classifiers can be used to create product types, portfolios, categories, and any other
  34. organizational structures your content may need.
  35. Implementation
  36. --------------
  37. Classifiers are enabled by default on all ``CoderedPage`` models. The filtering HTML UI
  38. is rendered in the ``{% block index_filters %}`` block on the page template, which originates
  39. in ``base.html`` but is overridden in various other templates such as ``web_page_notitle.html``
  40. and ``article_index_page.html``.
  41. Wagtail CRX provides two filtering templates by default, a Bootstrap nav in
  42. ``wagtailcrx/includes/classifier_nav.html`` and a simple select/dropdown form in
  43. ``wagtailcrx/includes/classifier_dropdowns.html``. Most likely, you will want to implement your
  44. own filtering UI based on your own website needs, but you can follow the example in these two
  45. templates.
  46. Classifiers are not limited to just Pages though - they can be used on Snippets or any other
  47. model (Snippet example below)::
  48. from django.db import models
  49. from modelcluster.fields import ParentalManyToManyField
  50. from wagtail.admin.edit_handlers import FieldPanel
  51. from wagtail.snippets.models import register_snippet
  52. @register_snippet
  53. class MySnippet(models.Model):
  54. name = models.CharField(
  55. max_length=255,
  56. )
  57. classifier_terms = ParentalManyToManyField(
  58. 'wagtailcrx.ClassifierTerm',
  59. blank=True,
  60. )
  61. panels = [
  62. FieldPanel('name')
  63. FieldPanel('classifier_terms'),
  64. ]
  65. This will create a default list of checkboxes or a multi-select in the Wagtail UI
  66. to select classifier terms. However, if you prefer to have the checkboxes grouped
  67. by the Classifier they belong to (same UI as the **Classify** tab in the page editor),
  68. use the built-in ``ClassifierSelectWidget``::
  69. from wagtailcrx.widgets import ClassifierSelectWidget
  70. panels = [
  71. FieldPanel('name')
  72. FieldPanel('classifier_terms', widget=ClassifierSelectWidget()),
  73. ]
  74. Finally run ``python manage.py makemigrations website`` and ``python manage.py migrate`` to
  75. create the new models in your project.