From d655e3542cdfde3fb0c214f8ed9c38db4c1e8924 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 19 Feb 2015 17:25:24 +0000 Subject: [PATCH] Use routablepage instead of route override --- .../static_site_generation.rst | 48 +++++++++++++------ 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/docs/contrib_components/static_site_generation.rst b/docs/contrib_components/static_site_generation.rst index f8914e4d97..ca48372a36 100644 --- a/docs/contrib_components/static_site_generation.rst +++ b/docs/contrib_components/static_site_generation.rst @@ -31,7 +31,7 @@ Then add ``django_medusa`` and ``wagtail.contrib.wagtailmedusa`` to ``INSTALLED_ Rendering ~~~~~~~~~ -To render a site, run ``./manage.py staticsitegen``. This will render the entire website and place the HTML in a folder called 'medusa_output'. The static and media folders need to be copied into this folder manually after the rendering is complete. This feature inherits django-medusa's ability to render your static site to Amazon S3 or Google App Engine; see the `medusa docs `_ for configuration details. +To render a site, run ``./manage.py staticsitegen``. This will render the entire website and place the HTML in a folder called 'medusa_output'. The static and media folders need to be copied into this folder manually after the rendering is complete. This feature inherits ``django-medusa``'s ability to render your static site to Amazon S3 or Google App Engine; see the `medusa docs `_ for configuration details. To test, open the 'medusa_output' folder in a terminal and run ``python -m SimpleHTTPServer``. @@ -39,31 +39,49 @@ To test, open the 'medusa_output' folder in a terminal and run ``python -m Simpl Advanced topics ~~~~~~~~~~~~~~~ -Replacing GET parameters with custom routing --------------------------------------------- +GET parameters +-------------- -Pages which require GET parameters (e.g. for pagination) don't generate suitable filenames for generated HTML files so they need to be changed to use custom routing instead. +Pages which require GET parameters (e.g. for pagination) don't generate suitable filenames for generated HTML files. -For example, let's say we have a Blog Index which uses pagination. We can override the ``route`` method to make it respond on urls like '/page/1', and pass the page number through to the ``serve`` method: +Wagtail provides a mixin (``wagtail.contrib.wagtailroutablepage.models.RoutablePageMixin``) which allows you to embed a Django url configuration into a page. This allows you to give the subpages a URL like ``/page/1/`` which work well with static site generation. + + +Example: .. code:: python - from wagtail.wagtailcore.url_routing import RouteResult + from wagtail.contrib.wagtailroutablepage.models import RoutablePageMixin - class BlogIndex(Page): + + class BlogIndex(Page, RoutablePageMixin): ... - def serve(self, request, page=1): + subpage_urls = ( + url(r'^$', 'serve_page', {'page': 1}), + url(r'^page/(?P\d+)/$', 'serve_page', name='page'), + ) + + def serve_page(self, request, page=1): ... - def route(self, request, path_components): - if self.live and len(path_components) == 2 and path_components[0] == 'page': - try: - return RouteResult(self, kwargs={'page': int(path_components[1])}) - except (TypeError, ValueError): - pass - return super(BlogIndex, self).route(request, path_components) +Then in the template, you can use the ``{% routablepageurl %}`` tag to link between the pages: + +..code:: html+Django + + {% load wagtailroutablepage_tags %} + + {% if results.has_previous %} + Next page + {% else %} + + {% if results.has_next %} + Next page + {% else %} + + +Next, you have to tell the ``wagtailmedusa`` module about your custom routing... Rendering pages which use custom routing