staticsitegen.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. Static site generator
  2. =====================
  3. This document describes how to render your Wagtail site into static HTML files on your local file system, Amazon S3 or Google App Engine, using `django medusa`_ and the ``wagtail.contrib.wagtailmedusa`` module.
  4. .. note::
  5. An alternative module based on the `django-bakery`_ package is available as a third-party contribution: https://github.com/mhnbcu/wagtailbakery
  6. Installing ``django-medusa``
  7. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. First, install ``django-medusa`` and ``django-sendfile`` from pip:
  9. .. code-block:: sh
  10. pip install django-medusa django-sendfile
  11. Then add ``django_medusa`` and ``wagtail.contrib.wagtailmedusa`` to ``INSTALLED_APPS``:
  12. .. code-block:: python
  13. INSTALLED_APPS = [
  14. ...
  15. 'django_medusa',
  16. 'wagtail.contrib.wagtailmedusa',
  17. ]
  18. Define ``MEDUSA_RENDERER_CLASS``, ``MEDUSA_DEPLOY_DIR`` and ``SENDFILE_BACKEND`` in settings:
  19. .. code-block:: python
  20. MEDUSA_RENDERER_CLASS = 'django_medusa.renderers.DiskStaticSiteRenderer'
  21. MEDUSA_DEPLOY_DIR = os.path.join(BASE_DIR, 'build')
  22. SENDFILE_BACKEND = 'sendfile.backends.simple'
  23. Rendering
  24. ~~~~~~~~~
  25. 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 <https://github.com/mtigas/django-medusa/blob/master/README.markdown>`_ for configuration details.
  26. To test, open the ``medusa_output`` folder in a terminal and run ``python -m SimpleHTTPServer``.
  27. Advanced topics
  28. ~~~~~~~~~~~~~~~
  29. GET parameters
  30. --------------
  31. Pages which require GET parameters (e.g. for pagination) don't generate a suitable file name for the generated HTML files.
  32. 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.
  33. Example:
  34. .. code-block:: python
  35. from wagtail.contrib.wagtailroutablepage.models import RoutablePageMixin, route
  36. class BlogIndex(Page, RoutablePageMixin):
  37. ...
  38. @route(r'^$', name='main')
  39. @route(r'^page/(?P<page>\d+)/$', name='page')
  40. def serve_page(self, request, page=1):
  41. ...
  42. Then in the template, you can use the ``{% routablepageurl %}`` tag to link between the pages:
  43. .. code-block:: html+django
  44. {% load wagtailroutablepage_tags %}
  45. {% if results.has_previous %}
  46. <a href="{% routablepageurl page 'page' results.previous_page_number %}">Next page</a>
  47. {% else %}
  48. {% if results.has_next %}
  49. <a href="{% routablepageurl page 'page' results.next_page_number %}">Next page</a>
  50. {% else %}
  51. Next, you have to tell the ``wagtailmedusa`` module about your custom routing...
  52. Rendering pages which use custom routing
  53. ----------------------------------------
  54. For page types that override the ``route`` method, we need to let ``django-medusa`` know which URLs it responds on. This is done by overriding the ``get_static_site_paths`` method to make it yield one string per URL path.
  55. For example, the BlogIndex above would need to yield one URL for each page of results:
  56. .. code-block:: python
  57. def get_static_site_paths(self):
  58. # Get page count
  59. page_count = ...
  60. # Yield a path for each page
  61. for page in range(page_count):
  62. yield '/%d/' % (page + 1)
  63. # Yield from superclass
  64. for path in super(BlogIndex, self).get_static_site_paths():
  65. yield path
  66. .. _django medusa: https://github.com/mtigas/django-medusa
  67. .. _django-bakery: https://github.com/datadesk/django-bakery