2014-08-21 00:08:29 +00:00
.. _routable_page_mixin:
2014-07-21 15:03:28 +00:00
2015-05-03 10:43:57 +00:00
=====================
`` RoutablePageMixin ``
=====================
2014-07-14 14:09:22 +00:00
2015-05-18 14:17:27 +00:00
.. module :: wagtail.contrib.wagtailroutablepage
2015-04-08 12:51:29 +00:00
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.
2014-07-21 14:52:19 +00:00
2015-04-08 12:51:29 +00:00
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).
2014-07-14 14:09:22 +00:00
The basics
==========
2015-04-27 13:14:35 +00:00
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 `` .
2014-07-14 14:09:22 +00:00
2014-07-21 14:52:19 +00:00
Here's an example of an `` EventPage `` with three views:
2014-07-14 14:09:22 +00:00
.. code-block :: python
2014-08-21 00:08:29 +00:00
from wagtail.wagtailcore.models import Page
2015-04-08 12:51:29 +00:00
from wagtail.contrib.wagtailroutablepage.models import RoutablePageMixin, route
2014-07-14 14:09:22 +00:00
2014-08-21 00:08:29 +00:00
class EventPage(RoutablePageMixin, Page):
2015-04-08 12:51:29 +00:00
...
2014-07-14 14:09:22 +00:00
2015-04-08 12:51:29 +00:00
@route(r'^$')
2014-07-14 14:09:22 +00:00
def current_events(self, request):
"""
View function for the current events page
"""
...
2015-04-08 12:51:29 +00:00
@route(r'^past/$')
2014-07-14 14:09:22 +00:00
def past_events(self, request):
"""
2014-08-19 09:51:52 +00:00
View function for the past events page
2014-07-14 14:09:22 +00:00
"""
...
2015-06-19 08:55:30 +00:00
# Multiple routes!
2015-04-08 12:51:29 +00:00
@route(r'^year/(\d+)/$')
2015-06-19 08:55:30 +00:00
@route(r'^year/current/$')
def events_for_year(self, request, year=None):
"""
View function for the events for year page
"""
...
Reversing URLs
==============
:class: `~models.RoutablePageMixin` adds a :meth: `~models.RoutablePageMixin.reverse_subpage` method to your page model which you can use for reversing URLs. For example:
.. code-block :: python
# The URL name defaults to the view method name.
>>> event_page.reverse_subpage('events_for_year', args=(2015, ))
'year/2015/'
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:
.. code-block :: python
>>> event_page.url + event_page.reverse_subpage('events_for_year', args=(2015, ))
'/events/year/2015/'
>>> event_page.full_url + event_page.reverse_subpage('events_for_year', args=(2015, ))
'http://example.com/events/year/2015/'
Changing route names
--------------------
The route name defaults to the name of the view. You can override this name with the `` name `` keyword argument on `` @route `` :
.. code-block :: python
from wagtail.wagtailcore.models import Page
from wagtail.contrib.wagtailroutablepage.models import RoutablePageMixin, route
class EventPage(RoutablePageMixin, Page):
...
@route(r'^year/(\d+)/$', name='year')
2015-04-08 12:51:29 +00:00
def events_for_year(self, request, year):
2014-07-14 14:09:22 +00:00
"""
View function for the events for year page
"""
...
2015-06-19 08:55:30 +00:00
.. code-block :: python
>>> event_page.reverse_subpage('year', args=(2015, ))
'/events/year/2015/'
2014-07-14 14:09:22 +00:00
2014-08-21 00:08:29 +00:00
The `` RoutablePageMixin `` class
===============================
2014-07-14 14:09:22 +00:00
.. automodule :: wagtail.contrib.wagtailroutablepage.models
2014-08-21 00:08:29 +00:00
.. autoclass :: RoutablePageMixin
2014-07-14 14:09:22 +00:00
2015-04-08 12:51:29 +00:00
.. automethod :: get_subpage_urls
2014-07-14 14:09:22 +00:00
.. automethod :: resolve_subpage
Example:
.. code-block :: python
2015-05-28 14:18:18 +00:00
view, args, kwargs = page.resolve_subpage('/past/')
2014-07-14 14:09:22 +00:00
response = view(request, *args, * *kwargs)
.. automethod :: reverse_subpage
Example:
.. code-block :: python
2015-05-28 14:18:18 +00:00
url = page.url + page.reverse_subpage('events_for_year', kwargs={'year': '2014'})
2014-08-14 00:28:50 +00:00
2015-04-09 13:07:53 +00:00
2014-08-18 08:18:15 +00:00
.. _routablepageurl_template_tag:
2014-08-14 00:28:50 +00:00
The `` routablepageurl `` template tag
====================================
.. currentmodule :: wagtail.contrib.wagtailroutablepage.templatetags.wagtailroutablepage_tags
.. autofunction :: routablepageurl
Example:
.. code-block :: html+django
{% load wagtailroutablepage_tags %}
2015-10-13 08:52:16 +00:00
{% routablepageurl page "feed" %}
{% routablepageurl page "archive" 2014 08 14 %}
{% routablepageurl page "food" foo="bar" baz="quux" %}