sitemaps.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. .. _sitemap_generation:
  2. Sitemap generator
  3. =================
  4. This document describes how to create XML sitemaps for your Wagtail website
  5. using the ``wagtail.contrib.sitemaps`` module.
  6. .. note::
  7. As of Wagtail 1.10 the Django contrib sitemap app is used to generate
  8. sitemaps. However since Wagtail requires the Site instance to be available
  9. during the sitemap generation you will have to use the views from the
  10. ``wagtail.contrib.sitemaps.views`` module instead of the views
  11. provided by Django (``django.contrib.sitemaps.views``).
  12. The usage of these views is otherwise identical, which means that
  13. customisation and caching of the sitemaps are done using the default Django
  14. patterns. See the Django documentation for in-depth information.
  15. Basic configuration
  16. ~~~~~~~~~~~~~~~~~~~
  17. You firstly need to add ``"django.contrib.sitemaps"`` to INSTALLED_APPS in your
  18. Django settings file:
  19. .. code-block:: python
  20. INSTALLED_APPS = [
  21. ...
  22. "django.contrib.sitemaps",
  23. ]
  24. Then, in ``urls.py``, you need to add a link to the
  25. ``wagtail.contrib.sitemaps.views.sitemap`` view which generates the
  26. sitemap:
  27. .. code-block:: python
  28. from wagtail.contrib.sitemaps.views import sitemap
  29. urlpatterns = [
  30. ...
  31. path('sitemap.xml', sitemap),
  32. ...
  33. # Ensure that the 'sitemap' line appears above the default Wagtail page serving route
  34. re_path(r'', include(wagtail_urls)),
  35. ]
  36. You should now be able to browse to ``/sitemap.xml`` and see the sitemap
  37. working. By default, all published pages in your website will be added to the
  38. site map.
  39. Setting the hostname
  40. ~~~~~~~~~~~~~~~~~~~~
  41. By default, the sitemap uses the hostname defined in the Wagtail Admin's
  42. ``Sites`` area. If your default site is called ``localhost``, then URLs in the
  43. sitemap will look like:
  44. .. code-block:: xml
  45. <url>
  46. <loc>http://localhost/about/</loc>
  47. <lastmod>2015-09-26</lastmod>
  48. </url>
  49. For tools like Google Search Tools to properly index your site, you need to set
  50. a valid, crawlable hostname. If you change the site's hostname from
  51. ``localhost`` to ``mysite.com``, ``sitemap.xml`` will contain the correct URLs:
  52. .. code-block:: xml
  53. <url>
  54. <loc>http://mysite.com/about/</loc>
  55. <lastmod>2015-09-26</lastmod>
  56. </url>
  57. If you change the site's port to ``443``, the ``https`` scheme will be used.
  58. Find out more about :ref:`working with Sites<site-model-ref>`.
  59. Customising
  60. ~~~~~~~~~~~
  61. URLs
  62. ----
  63. The ``Page`` class defines a ``get_sitemap_urls`` method which you can
  64. override to customise sitemaps per ``Page`` instance. This method must accept
  65. a request object and return a list of dictionaries, one dictionary per URL
  66. entry in the sitemap. You can exclude pages from the sitemap by returning an
  67. empty list.
  68. Each dictionary can contain the following:
  69. - **location** (required) - This is the full URL path to add into the sitemap.
  70. - **lastmod** - A python date or datetime set to when the page was last modified.
  71. - **changefreq**
  72. - **priority**
  73. You can add more but you will need to override the
  74. ``sitemap.xml`` template in order for them to be displayed in the sitemap.
  75. Serving multiple sitemaps
  76. ~~~~~~~~~~~~~~~~~~~~~~~~~
  77. If you want to support the sitemap indexes from Django then you will need to
  78. use the index view from ``wagtail.contrib.sitemaps.views`` instead of the index
  79. view from ``django.contrib.sitemaps.views``. Please see the Django
  80. documentation for further details.