2014-08-21 02:08:29 +02:00
.. _routable_page_mixin:
2014-07-21 17:03:28 +02:00
2014-07-15 14:48:05 +02:00
====================================
Embedding URL configuration in Pages
====================================
2014-07-14 16:09:22 +02:00
2014-08-21 02:08:29 +02: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 `` BlogIndex `` page.
2014-07-21 16:52:19 +02:00
2014-08-21 02:08:29 +02: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, using Django's urlconf scheme. If none of the patterns match, control is passed to subpages as usual (or failing that, a 404 error is thrown).
2014-07-14 16:09:22 +02:00
The basics
==========
2015-04-09 15:07:53 +02: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` , and configure the `` subpage_urls `` attribute with your URL configuration.
2014-07-14 16:09:22 +02:00
2014-07-21 16:52:19 +02:00
Here's an example of an `` EventPage `` with three views:
2014-07-14 16:09:22 +02:00
.. code-block :: python
from django.conf.urls import url
2014-08-21 02:08:29 +02:00
from wagtail.contrib.wagtailroutablepage.models import RoutablePageMixin
from wagtail.wagtailcore.models import Page
2014-07-14 16:09:22 +02:00
2014-08-21 02:08:29 +02:00
class EventPage(RoutablePageMixin, Page):
2015-04-09 15:07:53 +02:00
subpage_urls = (
url(r'^$', 'current_events', name='current_events'),
url(r'^past/$', 'past_events', name='past_events'),
url(r'^year/(\d+)/$', 'events_for_year', name='events_for_year'),
)
2014-07-14 16:09:22 +02:00
def current_events(self, request):
"""
View function for the current events page
"""
...
def past_events(self, request):
"""
2014-08-19 11:51:52 +02:00
View function for the past events page
2014-07-14 16:09:22 +02:00
"""
...
def events_for_year(self, request):
"""
View function for the events for year page
"""
...
2014-08-21 02:08:29 +02:00
The `` RoutablePageMixin `` class
===============================
2014-07-14 16:09:22 +02:00
.. automodule :: wagtail.contrib.wagtailroutablepage.models
2014-08-21 02:08:29 +02:00
.. autoclass :: RoutablePageMixin
2014-07-14 16:09:22 +02:00
2014-07-15 14:45:42 +02:00
.. autoattribute :: subpage_urls
2014-07-14 16:09:22 +02:00
2014-07-15 14:45:42 +02:00
Example:
.. code-block :: python
from django.conf.urls import url
2014-08-21 02:08:29 +02:00
from wagtail.wagtailcore.models import Page
2014-09-04 10:20:53 +02:00
2014-08-21 02:08:29 +02:00
class MyPage(RoutablePageMixin, Page):
2015-04-09 15:07:53 +02:00
subpage_urls = (
url(r'^$', 'main', name='main'),
url(r'^archive/$', 'archive', name='archive'),
url(r'^archive/(?P<year>[0-9]{4})/$', 'archive', name='archive'),
)
def main(self, request):
2014-08-18 12:40:48 +02:00
...
2015-04-09 15:07:53 +02:00
def archive(self, request, year=None):
2014-08-18 12:40:48 +02:00
...
2014-07-14 16:09:22 +02:00
.. automethod :: resolve_subpage
Example:
.. code-block :: python
2014-09-04 10:20:53 +02:00
view, args, kwargs = page.resolve_subpage('/archive/')
2014-07-14 16:09:22 +02:00
response = view(request, *args, * *kwargs)
.. automethod :: reverse_subpage
Example:
.. code-block :: python
2014-09-04 10:20:53 +02:00
url = page.url + page.reverse_subpage('archive', kwargs={'year': '2014'})
2014-08-14 02:28:50 +02:00
2015-04-09 15:07:53 +02:00
2014-08-18 10:18:15 +02:00
.. _routablepageurl_template_tag:
2014-08-14 02:28:50 +02:00
The `` routablepageurl `` template tag
====================================
.. currentmodule :: wagtail.contrib.wagtailroutablepage.templatetags.wagtailroutablepage_tags
.. autofunction :: routablepageurl
Example:
.. code-block :: html+django
{% load wagtailroutablepage_tags %}
{% routablepageurl self "feed" %}
{% routablepageurl self "archive" 2014 08 14 %}
{% routablepageurl self "food" foo="bar" baz="quux" %}