sitemaps.rst 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. .. _sitemap_generation:
  2. Sitemap generator
  3. =================
  4. This document describes how to create XML sitemaps for your Wagtail website using the ``wagtail.contrib.wagtailsitemaps`` module.
  5. Basic configuration
  6. ~~~~~~~~~~~~~~~~~~~
  7. You firstly need to add ``"wagtail.contrib.wagtailsitemaps"`` to INSTALLED_APPS in your Django settings file:
  8. .. code-block:: python
  9. INSTALLED_APPS = [
  10. ...
  11. "wagtail.contrib.wagtailsitemaps",
  12. ]
  13. Then, in ``urls.py``, you need to add a link to the ``wagtail.contrib.wagtailsitemaps.views.sitemap`` view which generates the sitemap:
  14. .. code-block:: python
  15. from wagtail.contrib.wagtailsitemaps.views import sitemap
  16. urlpatterns = [
  17. ...
  18. url('^sitemap\.xml$', sitemap),
  19. ]
  20. You should now be able to browse to ``/sitemap.xml`` and see the sitemap working. By default, all published pages in your website will be added to the site map.
  21. Setting the hostname
  22. ~~~~~~~~~~~~~~~~~~~~
  23. By default, the sitemap uses the hostname defined in the Wagtail Admin's ``Sites`` area. If your
  24. default site is called ``localhost``, then URLs in the sitemap will look like:
  25. .. code-block:: xml
  26. <url>
  27. <loc>http://localhost/about/</loc>
  28. <lastmod>2015-09-26</lastmod>
  29. </url>
  30. For tools like Google Search Tools to properly index your site, you need to set a valid, crawlable hostname. If you change the site's hostname from ``localhost`` to ``mysite.com``, ``sitemap.xml``
  31. will contain the correct URLs:
  32. .. code-block:: xml
  33. <url>
  34. <loc>http://mysite.com/about/</loc>
  35. <lastmod>2015-09-26</lastmod>
  36. </url>
  37. Find out more about `working with Sites
  38. </reference/pages/model_reference.html?highlight=site#site>`.
  39. Customising
  40. ~~~~~~~~~~~
  41. URLs
  42. ----
  43. The ``Page`` class defines a ``get_sitemap_urls`` method which you can override to customise sitemaps per ``Page`` instance. This method must return a list of dictionaries, one dictionary per URL entry in the sitemap. You can exclude pages from the sitemap by returning an empty list.
  44. Each dictionary can contain the following:
  45. - **location** (required) - This is the full URL path to add into the sitemap.
  46. - **lastmod** - A python date or datetime set to when the page was last modified.
  47. - **changefreq**
  48. - **priority**
  49. You can add more but you will need to override the ``wagtailsitemaps/sitemap.xml`` template in order for them to be displayed in the sitemap.
  50. Cache
  51. -----
  52. By default, sitemaps are cached for 100 minutes. You can change this by setting ``WAGTAILSITEMAPS_CACHE_TIMEOUT`` in your Django settings to the number of seconds you would like the cache to last for.