routablepage.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. .. _routable_page_mixin:
  2. =====================
  3. ``RoutablePageMixin``
  4. =====================
  5. .. module:: wagtail.contrib.wagtailroutablepage
  6. The ``RoutablePageMixin`` mixin provides a convenient way for a page to respond on multiple sub-URLs with different views. For example, a blog section on a site might provide several different types of index page at URLs like ``/blog/2013/06/``, ``/blog/authors/bob/``, ``/blog/tagged/python/``, all served by the same page instance.
  7. A ``Page`` using ``RoutablePageMixin`` exists within the page tree like any other page, but URL paths underneath it are checked against a list of patterns. If none of the patterns match, control is passed to subpages as usual (or failing that, a 404 error is thrown).
  8. Installation
  9. ============
  10. Add ``"wagtail.contrib.wagtailroutablepage"`` to your INSTALLED_APPS:
  11. .. code-block:: python
  12. INSTALLED_APPS = [
  13. ...
  14. "wagtail.contrib.wagtailroutablepage",
  15. ]
  16. The basics
  17. ==========
  18. To use ``RoutablePageMixin``, you need to make your class inherit from both :class:`wagtail.contrib.wagtailroutablepage.models.RoutablePageMixin` and :class:`wagtail.wagtailcore.models.Page`, then define some view methods and decorate them with ``wagtail.contrib.wagtailroutablepage.models.route``.
  19. Here's an example of an ``EventPage`` with three views:
  20. .. code-block:: python
  21. from wagtail.wagtailcore.models import Page
  22. from wagtail.contrib.wagtailroutablepage.models import RoutablePageMixin, route
  23. class EventPage(RoutablePageMixin, Page):
  24. ...
  25. @route(r'^$')
  26. def current_events(self, request):
  27. """
  28. View function for the current events page
  29. """
  30. ...
  31. @route(r'^past/$')
  32. def past_events(self, request):
  33. """
  34. View function for the past events page
  35. """
  36. ...
  37. # Multiple routes!
  38. @route(r'^year/(\d+)/$')
  39. @route(r'^year/current/$')
  40. def events_for_year(self, request, year=None):
  41. """
  42. View function for the events for year page
  43. """
  44. ...
  45. Reversing URLs
  46. ==============
  47. :class:`~models.RoutablePageMixin` adds a :meth:`~models.RoutablePageMixin.reverse_subpage` method to your page model which you can use for reversing URLs. For example:
  48. .. code-block:: python
  49. # The URL name defaults to the view method name.
  50. >>> event_page.reverse_subpage('events_for_year', args=(2015, ))
  51. 'year/2015/'
  52. This method only returns the part of the URL within the page. To get the full URL, you must append it to the values of either the :attr:`~wagtail.wagtailcore.models.Page.url` or the :attr:`~wagtail.wagtailcore.models.Page.full_url` attribute on your page:
  53. .. code-block:: python
  54. >>> event_page.url + event_page.reverse_subpage('events_for_year', args=(2015, ))
  55. '/events/year/2015/'
  56. >>> event_page.full_url + event_page.reverse_subpage('events_for_year', args=(2015, ))
  57. 'http://example.com/events/year/2015/'
  58. Changing route names
  59. --------------------
  60. The route name defaults to the name of the view. You can override this name with the ``name`` keyword argument on ``@route``:
  61. .. code-block:: python
  62. from wagtail.wagtailcore.models import Page
  63. from wagtail.contrib.wagtailroutablepage.models import RoutablePageMixin, route
  64. class EventPage(RoutablePageMixin, Page):
  65. ...
  66. @route(r'^year/(\d+)/$', name='year')
  67. def events_for_year(self, request, year):
  68. """
  69. View function for the events for year page
  70. """
  71. ...
  72. .. code-block:: python
  73. >>> event_page.reverse_subpage('year', args=(2015, ))
  74. '/events/year/2015/'
  75. The ``RoutablePageMixin`` class
  76. ===============================
  77. .. automodule:: wagtail.contrib.wagtailroutablepage.models
  78. .. autoclass:: RoutablePageMixin
  79. .. automethod:: get_subpage_urls
  80. .. automethod:: resolve_subpage
  81. Example:
  82. .. code-block:: python
  83. view, args, kwargs = page.resolve_subpage('/past/')
  84. response = view(request, *args, **kwargs)
  85. .. automethod:: reverse_subpage
  86. Example:
  87. .. code-block:: python
  88. url = page.url + page.reverse_subpage('events_for_year', kwargs={'year': '2014'})
  89. .. _routablepageurl_template_tag:
  90. The ``routablepageurl`` template tag
  91. ====================================
  92. .. currentmodule:: wagtail.contrib.wagtailroutablepage.templatetags.wagtailroutablepage_tags
  93. .. autofunction:: routablepageurl
  94. Example:
  95. .. code-block:: html+django
  96. {% load wagtailroutablepage_tags %}
  97. {% routablepageurl page "feed" %}
  98. {% routablepageurl page "archive" 2014 08 14 %}
  99. {% routablepageurl page "food" foo="bar" baz="quux" %}