Fixed #27395 -- Added sitemap 'alternates' generation.

Updated the sitemap generator and default template to optionally
include link elements with hreflang attribute to alternate language
URLs.
This commit is contained in:
Florian Demmer 2020-07-29 10:33:20 +02:00 committed by Carlton Gibson
parent 50e1ccbbea
commit 16218c2060
7 changed files with 263 additions and 42 deletions

View file

@ -252,6 +252,40 @@ Note:
be generated using all of your :setting:`LANGUAGES`. The default is
``False``.
.. attribute:: Sitemap.languages
.. versionadded:: 3.2
**Optional.**
A :term:`sequence` of :term:`language codes<language code>` to use for
generating alternate links when :attr:`~Sitemap.i18n` is enabled.
Defaults to :setting:`LANGUAGES`.
.. attribute:: Sitemap.alternates
.. versionadded:: 3.2
**Optional.**
A boolean attribute. When used in conjunction with
:attr:`~Sitemap.i18n` generated URLs will each have a list of alternate
links pointing to other language versions using the `hreflang
attribute`_. The default is ``False``.
.. _hreflang attribute: https://support.google.com/webmasters/answer/189077
.. attribute:: Sitemap.x_default
.. versionadded:: 3.2
**Optional.**
A boolean attribute. When ``True`` the alternate links generated by
:attr:`~Sitemap.alternates` will contain a ``hreflang="x-default"``
fallback entry with a value of :setting:`LANGUAGE_CODE`. The default is
``False``.
Shortcuts
=========
@ -438,12 +472,22 @@ The variable ``urlset`` is a list of URLs that should appear in the
sitemap. Each URL exposes attributes as defined in the
:class:`~django.contrib.sitemaps.Sitemap` class:
- ``alternates``
- ``changefreq``
- ``item``
- ``lastmod``
- ``location``
- ``priority``
The ``alternates`` attribute is available when :attr:`~Sitemap.i18n` and
:attr:`~Sitemap.alternates` are enabled. It is a list of other language
versions, including the optional :attr:`~Sitemap.x_default` fallback, for each
URL. Each alternate is a dictionary with ``location`` and ``lang_code`` keys.
.. versionchanged:: 3.2
The ``alternates`` attribute was added.
The ``item`` attribute has been added for each URL to allow more flexible
customization of the templates, such as `Google news sitemaps`_. Assuming
Sitemap's :attr:`~Sitemap.items()` would return a list of items with