2008-08-23 22:25:40 +00:00
|
|
|
=====================
|
|
|
|
The sitemap framework
|
|
|
|
=====================
|
|
|
|
|
|
|
|
.. module:: django.contrib.sitemaps
|
|
|
|
:synopsis: A framework for generating Google sitemap XML files.
|
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
Django comes with a high-level sitemap-generating framework to create sitemap_
|
|
|
|
XML files.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2017-05-20 15:51:21 +00:00
|
|
|
.. _sitemap: https://www.sitemaps.org/
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
Overview
|
|
|
|
========
|
|
|
|
|
2015-11-15 12:05:15 +00:00
|
|
|
A sitemap is an XML file on your website that tells search-engine indexers how
|
2008-08-23 22:25:40 +00:00
|
|
|
frequently your pages change and how "important" certain pages are in relation
|
|
|
|
to other pages on your site. This information helps search engines index your
|
|
|
|
site.
|
|
|
|
|
|
|
|
The Django sitemap framework automates the creation of this XML file by letting
|
|
|
|
you express this information in Python code.
|
|
|
|
|
2010-08-19 19:27:44 +00:00
|
|
|
It works much like Django's :doc:`syndication framework
|
2019-06-17 14:54:55 +00:00
|
|
|
</ref/contrib/syndication>`. To create a sitemap, write a
|
2008-08-23 22:25:40 +00:00
|
|
|
:class:`~django.contrib.sitemaps.Sitemap` class and point to it in your
|
2010-08-19 19:27:44 +00:00
|
|
|
:doc:`URLconf </topics/http/urls>`.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
Installation
|
|
|
|
============
|
|
|
|
|
|
|
|
To install the sitemap app, follow these steps:
|
|
|
|
|
2018-11-15 18:54:28 +00:00
|
|
|
#. Add ``'django.contrib.sitemaps'`` to your :setting:`INSTALLED_APPS` setting.
|
2010-10-23 16:37:51 +00:00
|
|
|
|
2018-11-15 18:54:28 +00:00
|
|
|
#. Make sure your :setting:`TEMPLATES` setting contains a ``DjangoTemplates``
|
2014-12-17 21:10:57 +00:00
|
|
|
backend whose ``APP_DIRS`` options is set to ``True``. It's in there by
|
|
|
|
default, so you'll only need to change this if you've changed that setting.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2018-11-15 18:54:28 +00:00
|
|
|
#. Make sure you've installed the :mod:`sites framework<django.contrib.sites>`.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
(Note: The sitemap application doesn't install any database tables. The only
|
|
|
|
reason it needs to go into :setting:`INSTALLED_APPS` is so that the
|
2009-12-14 12:08:23 +00:00
|
|
|
:func:`~django.template.loaders.app_directories.Loader` template
|
2008-08-23 22:25:40 +00:00
|
|
|
loader can find the default templates.)
|
|
|
|
|
|
|
|
Initialization
|
|
|
|
==============
|
|
|
|
|
2014-08-14 01:44:16 +00:00
|
|
|
.. function:: views.sitemap(request, sitemaps, section=None, template_name='sitemap.xml', content_type='application/xml')
|
2013-01-01 13:12:42 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
To activate sitemap generation on your Django site, add this line to your
|
2010-08-19 19:27:44 +00:00
|
|
|
:doc:`URLconf </topics/http/urls>`::
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2014-08-12 14:54:42 +00:00
|
|
|
from django.contrib.sitemaps.views import sitemap
|
|
|
|
|
2016-10-20 17:29:04 +00:00
|
|
|
path(
|
|
|
|
"sitemap.xml",
|
|
|
|
sitemap,
|
|
|
|
{"sitemaps": sitemaps},
|
|
|
|
name="django.contrib.sitemaps.views.sitemap",
|
|
|
|
)
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
This tells Django to build a sitemap when a client accesses :file:`/sitemap.xml`.
|
|
|
|
|
|
|
|
The name of the sitemap file is not important, but the location is. Search
|
|
|
|
engines will only index links in your sitemap for the current URL level and
|
|
|
|
below. For instance, if :file:`sitemap.xml` lives in your root directory, it may
|
|
|
|
reference any URL in your site. However, if your sitemap lives at
|
|
|
|
:file:`/content/sitemap.xml`, it may only reference URLs that begin with
|
|
|
|
:file:`/content/`.
|
|
|
|
|
|
|
|
The sitemap view takes an extra, required argument: ``{'sitemaps': sitemaps}``.
|
|
|
|
``sitemaps`` should be a dictionary that maps a short section label (e.g.,
|
|
|
|
``blog`` or ``news``) to its :class:`~django.contrib.sitemaps.Sitemap` class
|
|
|
|
(e.g., ``BlogSitemap`` or ``NewsSitemap``). It may also map to an *instance* of
|
|
|
|
a :class:`~django.contrib.sitemaps.Sitemap` class (e.g.,
|
|
|
|
``BlogSitemap(some_var)``).
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``Sitemap`` classes
|
|
|
|
===================
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
A :class:`~django.contrib.sitemaps.Sitemap` class is a Python class that
|
|
|
|
represents a "section" of entries in your sitemap. For example, one
|
|
|
|
:class:`~django.contrib.sitemaps.Sitemap` class could represent all the entries
|
2021-07-23 06:48:16 +00:00
|
|
|
of your blog, while another could represent all of the events in your events
|
2019-06-17 14:54:55 +00:00
|
|
|
calendar.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
In the simplest case, all these sections get lumped together into one
|
|
|
|
:file:`sitemap.xml`, but it's also possible to use the framework to generate a
|
|
|
|
sitemap index that references individual sitemap files, one per section. (See
|
|
|
|
`Creating a sitemap index`_ below.)
|
|
|
|
|
|
|
|
:class:`~django.contrib.sitemaps.Sitemap` classes must subclass
|
|
|
|
``django.contrib.sitemaps.Sitemap``. They can live anywhere in your codebase.
|
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
An example
|
|
|
|
==========
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
Let's assume you have a blog system, with an ``Entry`` model, and you want your
|
|
|
|
sitemap to include all the links to your individual blog entries. Here's how
|
|
|
|
your sitemap class might look::
|
|
|
|
|
|
|
|
from django.contrib.sitemaps import Sitemap
|
2010-10-19 00:10:22 +00:00
|
|
|
from blog.models import Entry
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
class BlogSitemap(Sitemap):
|
|
|
|
changefreq = "never"
|
|
|
|
priority = 0.5
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
return Entry.objects.filter(is_draft=False)
|
|
|
|
|
|
|
|
def lastmod(self, obj):
|
|
|
|
return obj.pub_date
|
|
|
|
|
|
|
|
Note:
|
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* :attr:`~Sitemap.changefreq` and :attr:`~Sitemap.priority` are class
|
|
|
|
attributes corresponding to ``<changefreq>`` and ``<priority>`` elements,
|
|
|
|
respectively. They can be made callable as functions, as
|
|
|
|
:attr:`~Sitemap.lastmod` was in the example.
|
2019-09-30 12:52:17 +00:00
|
|
|
* :attr:`~Sitemap.items()` is a method that returns a :term:`sequence` or
|
|
|
|
``QuerySet`` of objects. The objects returned will get passed to any callable
|
|
|
|
methods corresponding to a sitemap property (:attr:`~Sitemap.location`,
|
|
|
|
:attr:`~Sitemap.lastmod`, :attr:`~Sitemap.changefreq`, and
|
|
|
|
:attr:`~Sitemap.priority`).
|
2016-03-09 15:00:27 +00:00
|
|
|
* :attr:`~Sitemap.lastmod` should return a :class:`~datetime.datetime`.
|
2011-10-14 00:12:01 +00:00
|
|
|
* There is no :attr:`~Sitemap.location` method in this example, but you
|
|
|
|
can provide it in order to specify the URL for your object. By default,
|
|
|
|
:attr:`~Sitemap.location()` calls ``get_absolute_url()`` on each object
|
|
|
|
and returns the result.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``Sitemap`` class reference
|
|
|
|
===========================
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
.. class:: Sitemap
|
|
|
|
|
|
|
|
A ``Sitemap`` class can define the following methods/attributes:
|
|
|
|
|
|
|
|
.. attribute:: Sitemap.items
|
|
|
|
|
2019-09-30 12:52:17 +00:00
|
|
|
**Required.** A method that returns a :term:`sequence` or ``QuerySet``
|
|
|
|
of objects. The framework doesn't care what *type* of objects they are;
|
|
|
|
all that matters is that these objects get passed to the
|
|
|
|
:attr:`~Sitemap.location()`, :attr:`~Sitemap.lastmod()`,
|
|
|
|
:attr:`~Sitemap.changefreq()` and :attr:`~Sitemap.priority()` methods.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
.. attribute:: Sitemap.location
|
|
|
|
|
|
|
|
**Optional.** Either a method or attribute.
|
|
|
|
|
2010-11-07 01:42:55 +00:00
|
|
|
If it's a method, it should return the absolute path for a given object
|
|
|
|
as returned by :attr:`~Sitemap.items()`.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2010-11-07 01:42:55 +00:00
|
|
|
If it's an attribute, its value should be a string representing an
|
|
|
|
absolute path to use for *every* object returned by
|
|
|
|
:attr:`~Sitemap.items()`.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2010-11-07 01:42:55 +00:00
|
|
|
In both cases, "absolute path" means a URL that doesn't include the
|
|
|
|
protocol or domain. Examples:
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2022-08-09 12:50:05 +00:00
|
|
|
* Good: ``'/foo/bar/'``
|
|
|
|
* Bad: ``'example.com/foo/bar/'``
|
|
|
|
* Bad: ``'https://example.com/foo/bar/'``
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2010-11-07 01:42:55 +00:00
|
|
|
If :attr:`~Sitemap.location` isn't provided, the framework will call
|
|
|
|
the ``get_absolute_url()`` method on each object as returned by
|
2008-08-23 22:25:40 +00:00
|
|
|
:attr:`~Sitemap.items()`.
|
|
|
|
|
2012-01-29 19:24:32 +00:00
|
|
|
To specify a protocol other than ``'http'``, use
|
|
|
|
:attr:`~Sitemap.protocol`.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. attribute:: Sitemap.lastmod
|
|
|
|
|
|
|
|
**Optional.** Either a method or attribute.
|
|
|
|
|
2016-03-09 15:00:27 +00:00
|
|
|
If it's a method, it should take one argument -- an object as returned
|
|
|
|
by :attr:`~Sitemap.items()` -- and return that object's last-modified
|
|
|
|
date/time as a :class:`~datetime.datetime`.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2016-03-09 15:00:27 +00:00
|
|
|
If it's an attribute, its value should be a :class:`~datetime.datetime`
|
2008-08-23 22:25:40 +00:00
|
|
|
representing the last-modified date/time for *every* object returned by
|
|
|
|
:attr:`~Sitemap.items()`.
|
|
|
|
|
2013-07-23 14:25:21 +00:00
|
|
|
If all items in a sitemap have a :attr:`~Sitemap.lastmod`, the sitemap
|
|
|
|
generated by :func:`views.sitemap` will have a ``Last-Modified``
|
|
|
|
header equal to the latest ``lastmod``. You can activate the
|
|
|
|
:class:`~django.middleware.http.ConditionalGetMiddleware` to make
|
|
|
|
Django respond appropriately to requests with an ``If-Modified-Since``
|
|
|
|
header which will prevent sending the sitemap if it hasn't changed.
|
|
|
|
|
2021-06-28 13:01:26 +00:00
|
|
|
.. attribute:: Sitemap.paginator
|
|
|
|
|
|
|
|
**Optional.**
|
|
|
|
|
|
|
|
This property returns a :class:`~django.core.paginator.Paginator` for
|
|
|
|
:attr:`~Sitemap.items()`. If you generate sitemaps in a batch you may
|
|
|
|
want to override this as a cached property in order to avoid multiple
|
|
|
|
``items()`` calls.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. attribute:: Sitemap.changefreq
|
|
|
|
|
|
|
|
**Optional.** Either a method or attribute.
|
|
|
|
|
2016-03-09 15:00:27 +00:00
|
|
|
If it's a method, it should take one argument -- an object as returned
|
|
|
|
by :attr:`~Sitemap.items()` -- and return that object's change
|
|
|
|
frequency as a string.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2016-03-09 15:00:27 +00:00
|
|
|
If it's an attribute, its value should be a string representing the
|
|
|
|
change frequency of *every* object returned by :attr:`~Sitemap.items()`.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2016-03-09 15:00:27 +00:00
|
|
|
Possible values for :attr:`~Sitemap.changefreq`, whether you use a
|
|
|
|
method or attribute, are:
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* ``'always'``
|
|
|
|
* ``'hourly'``
|
|
|
|
* ``'daily'``
|
|
|
|
* ``'weekly'``
|
|
|
|
* ``'monthly'``
|
|
|
|
* ``'yearly'``
|
|
|
|
* ``'never'``
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2012-01-29 19:24:32 +00:00
|
|
|
.. attribute:: Sitemap.priority
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
**Optional.** Either a method or attribute.
|
|
|
|
|
2016-03-09 15:00:27 +00:00
|
|
|
If it's a method, it should take one argument -- an object as returned
|
|
|
|
by :attr:`~Sitemap.items()` -- and return that object's priority as
|
|
|
|
either a string or float.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2016-03-09 15:00:27 +00:00
|
|
|
If it's an attribute, its value should be either a string or float
|
|
|
|
representing the priority of *every* object returned by
|
|
|
|
:attr:`~Sitemap.items()`.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2016-03-09 15:00:27 +00:00
|
|
|
Example values for :attr:`~Sitemap.priority`: ``0.4``, ``1.0``. The
|
|
|
|
default priority of a page is ``0.5``. See the `sitemaps.org
|
|
|
|
documentation`_ for more.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2017-05-20 15:51:21 +00:00
|
|
|
.. _sitemaps.org documentation: https://www.sitemaps.org/protocol.html#prioritydef
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2012-01-29 19:24:32 +00:00
|
|
|
.. attribute:: Sitemap.protocol
|
|
|
|
|
|
|
|
**Optional.**
|
|
|
|
|
|
|
|
This attribute defines the protocol (``'http'`` or ``'https'``) of the
|
|
|
|
URLs in the sitemap. If it isn't set, the protocol with which the
|
|
|
|
sitemap was requested is used. If the sitemap is built outside the
|
2023-01-06 11:55:14 +00:00
|
|
|
context of a request, the default is ``'https'``.
|
2012-01-29 19:24:32 +00:00
|
|
|
|
2023-01-06 11:55:14 +00:00
|
|
|
.. versionchanged:: 5.0
|
2021-05-15 06:11:14 +00:00
|
|
|
|
2023-01-06 11:55:14 +00:00
|
|
|
In older versions, the default protocol for sitemaps built outside
|
|
|
|
the context of a request was ``'http'``.
|
2021-05-15 06:11:14 +00:00
|
|
|
|
2015-05-04 17:44:00 +00:00
|
|
|
.. attribute:: Sitemap.limit
|
|
|
|
|
|
|
|
**Optional.**
|
|
|
|
|
|
|
|
This attribute defines the maximum number of URLs included on each page
|
|
|
|
of the sitemap. Its value should not exceed the default value of
|
|
|
|
``50000``, which is the upper limit allowed in the `Sitemaps protocol
|
2017-05-20 15:51:21 +00:00
|
|
|
<https://www.sitemaps.org/protocol.html#index>`_.
|
2015-05-04 17:44:00 +00:00
|
|
|
|
2014-06-06 18:47:15 +00:00
|
|
|
.. attribute:: Sitemap.i18n
|
|
|
|
|
|
|
|
**Optional.**
|
|
|
|
|
|
|
|
A boolean attribute that defines if the URLs of this sitemap should
|
|
|
|
be generated using all of your :setting:`LANGUAGES`. The default is
|
|
|
|
``False``.
|
2012-01-29 19:24:32 +00:00
|
|
|
|
2020-07-29 08:33:20 +00:00
|
|
|
.. attribute:: Sitemap.languages
|
|
|
|
|
|
|
|
**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
|
|
|
|
|
|
|
|
**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``.
|
|
|
|
|
2021-04-27 11:09:00 +00:00
|
|
|
.. _hreflang attribute: https://developers.google.com/search/docs/advanced/crawling/localized-versions
|
2020-07-29 08:33:20 +00:00
|
|
|
|
|
|
|
.. attribute:: Sitemap.x_default
|
|
|
|
|
|
|
|
**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``.
|
|
|
|
|
2020-12-30 16:44:53 +00:00
|
|
|
.. method:: Sitemap.get_latest_lastmod()
|
|
|
|
|
|
|
|
**Optional.** A method that returns the latest value returned by
|
|
|
|
:attr:`~Sitemap.lastmod`. This function is used to add the ``lastmod``
|
|
|
|
attribute to :ref:`Sitemap index context
|
|
|
|
variables<sitemap-index-context-variables>`.
|
|
|
|
|
|
|
|
By default :meth:`~Sitemap.get_latest_lastmod` returns:
|
|
|
|
|
|
|
|
* If :attr:`~Sitemap.lastmod` is an attribute:
|
|
|
|
:attr:`~Sitemap.lastmod`.
|
|
|
|
* If :attr:`~Sitemap.lastmod` is a method:
|
|
|
|
The latest ``lastmod`` returned by calling the method with all
|
|
|
|
items returned by :meth:`Sitemap.items`.
|
|
|
|
|
2023-04-27 17:53:26 +00:00
|
|
|
.. method:: Sitemap.get_languages_for_item(item)
|
2022-10-21 15:36:44 +00:00
|
|
|
|
|
|
|
.. versionadded:: 4.2
|
|
|
|
|
|
|
|
**Optional.** A method that returns the sequence of language codes for
|
|
|
|
which the item is displayed. By default
|
|
|
|
:meth:`~Sitemap.get_languages_for_item` returns
|
|
|
|
:attr:`~Sitemap.languages`.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Shortcuts
|
|
|
|
=========
|
|
|
|
|
2015-01-17 19:14:15 +00:00
|
|
|
The sitemap framework provides a convenience class for a common case:
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2017-02-15 09:36:18 +00:00
|
|
|
.. class:: GenericSitemap(info_dict, priority=None, changefreq=None, protocol=None)
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2012-04-25 19:17:47 +00:00
|
|
|
The :class:`django.contrib.sitemaps.GenericSitemap` class allows you to
|
|
|
|
create a sitemap by passing it a dictionary which has to contain at least
|
2013-01-01 13:12:42 +00:00
|
|
|
a ``queryset`` entry. This queryset will be used to generate the items
|
|
|
|
of the sitemap. It may also have a ``date_field`` entry that
|
|
|
|
specifies a date field for objects retrieved from the ``queryset``.
|
2020-12-30 16:44:53 +00:00
|
|
|
This will be used for the :attr:`~Sitemap.lastmod` attribute and
|
|
|
|
:meth:`~Sitemap.get_latest_lastmod` methods in the in the
|
2017-02-15 09:36:18 +00:00
|
|
|
generated sitemap.
|
|
|
|
|
|
|
|
The :attr:`~Sitemap.priority`, :attr:`~Sitemap.changefreq`,
|
|
|
|
and :attr:`~Sitemap.protocol` keyword arguments allow specifying these
|
|
|
|
attributes for all URLs.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
2014-12-07 23:21:07 +00:00
|
|
|
Here's an example of a :doc:`URLconf </topics/http/urls>` using
|
|
|
|
:class:`GenericSitemap`::
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2014-12-07 23:21:07 +00:00
|
|
|
from django.contrib.sitemaps import GenericSitemap
|
2014-08-12 14:54:42 +00:00
|
|
|
from django.contrib.sitemaps.views import sitemap
|
2016-10-20 17:29:04 +00:00
|
|
|
from django.urls import path
|
2010-10-19 00:10:22 +00:00
|
|
|
from blog.models import Entry
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
info_dict = {
|
|
|
|
"queryset": Entry.objects.all(),
|
|
|
|
"date_field": "pub_date",
|
|
|
|
}
|
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
urlpatterns = [
|
2008-08-23 22:25:40 +00:00
|
|
|
# some generic view using info_dict
|
|
|
|
# ...
|
|
|
|
# the sitemap
|
2016-10-20 17:29:04 +00:00
|
|
|
path(
|
|
|
|
"sitemap.xml",
|
|
|
|
sitemap,
|
|
|
|
{"sitemaps": {"blog": GenericSitemap(info_dict, priority=0.6)}},
|
|
|
|
name="django.contrib.sitemaps.views.sitemap",
|
|
|
|
),
|
2014-04-02 00:46:34 +00:00
|
|
|
]
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
.. _URLconf: ../url_dispatch/
|
|
|
|
|
2013-05-18 12:22:40 +00:00
|
|
|
Sitemap for static views
|
|
|
|
========================
|
|
|
|
|
|
|
|
Often you want the search engine crawlers to index views which are neither
|
|
|
|
object detail pages nor flatpages. The solution is to explicitly list URL
|
2015-12-30 15:51:16 +00:00
|
|
|
names for these views in ``items`` and call :func:`~django.urls.reverse` in
|
|
|
|
the ``location`` method of the sitemap. For example::
|
2013-05-18 12:22:40 +00:00
|
|
|
|
|
|
|
# sitemaps.py
|
|
|
|
from django.contrib import sitemaps
|
2015-12-30 15:51:16 +00:00
|
|
|
from django.urls import reverse
|
2013-05-18 12:22:40 +00:00
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2013-05-18 12:22:40 +00:00
|
|
|
class StaticViewSitemap(sitemaps.Sitemap):
|
|
|
|
priority = 0.5
|
|
|
|
changefreq = "daily"
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
return ["main", "about", "license"]
|
|
|
|
|
|
|
|
def location(self, item):
|
|
|
|
return reverse(item)
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2013-05-18 12:22:40 +00:00
|
|
|
# urls.py
|
2014-09-05 01:04:53 +00:00
|
|
|
from django.contrib.sitemaps.views import sitemap
|
2016-10-20 17:29:04 +00:00
|
|
|
from django.urls import path
|
2014-09-05 01:04:53 +00:00
|
|
|
|
2013-05-18 12:22:40 +00:00
|
|
|
from .sitemaps import StaticViewSitemap
|
2014-09-05 01:04:53 +00:00
|
|
|
from . import views
|
2013-05-18 12:22:40 +00:00
|
|
|
|
|
|
|
sitemaps = {
|
|
|
|
"static": StaticViewSitemap,
|
|
|
|
}
|
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
urlpatterns = [
|
2016-10-20 17:29:04 +00:00
|
|
|
path("", views.main, name="main"),
|
|
|
|
path("about/", views.about, name="about"),
|
|
|
|
path("license/", views.license, name="license"),
|
2013-05-18 12:22:40 +00:00
|
|
|
# ...
|
2016-10-20 17:29:04 +00:00
|
|
|
path(
|
|
|
|
"sitemap.xml",
|
|
|
|
sitemap,
|
|
|
|
{"sitemaps": sitemaps},
|
|
|
|
name="django.contrib.sitemaps.views.sitemap",
|
2023-02-28 19:53:28 +00:00
|
|
|
),
|
2014-04-02 00:46:34 +00:00
|
|
|
]
|
2013-05-18 12:22:40 +00:00
|
|
|
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Creating a sitemap index
|
|
|
|
========================
|
|
|
|
|
2014-08-14 01:44:16 +00:00
|
|
|
.. function:: views.index(request, sitemaps, template_name='sitemap_index.xml', content_type='application/xml', sitemap_url_name='django.contrib.sitemaps.views.sitemap')
|
2013-01-01 13:12:42 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
The sitemap framework also has the ability to create a sitemap index that
|
|
|
|
references individual sitemap files, one per each section defined in your
|
2013-01-01 13:12:42 +00:00
|
|
|
``sitemaps`` dictionary. The only differences in usage are:
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* You use two views in your URLconf: :func:`django.contrib.sitemaps.views.index`
|
|
|
|
and :func:`django.contrib.sitemaps.views.sitemap`.
|
|
|
|
* The :func:`django.contrib.sitemaps.views.sitemap` view should take a
|
2013-01-01 13:12:42 +00:00
|
|
|
``section`` keyword argument.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2008-09-07 22:56:03 +00:00
|
|
|
Here's what the relevant URLconf lines would look like for the example above::
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
from django.contrib.sitemaps import views
|
|
|
|
|
|
|
|
urlpatterns = [
|
2021-10-21 14:09:29 +00:00
|
|
|
path(
|
|
|
|
"sitemap.xml",
|
|
|
|
views.index,
|
|
|
|
{"sitemaps": sitemaps},
|
|
|
|
name="django.contrib.sitemaps.views.index",
|
|
|
|
),
|
2016-10-20 17:29:04 +00:00
|
|
|
path(
|
|
|
|
"sitemap-<section>.xml",
|
|
|
|
views.sitemap,
|
|
|
|
{"sitemaps": sitemaps},
|
|
|
|
name="django.contrib.sitemaps.views.sitemap",
|
|
|
|
),
|
2014-04-02 00:46:34 +00:00
|
|
|
]
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-06-28 10:16:34 +00:00
|
|
|
This will automatically generate a :file:`sitemap.xml` file that references
|
|
|
|
both :file:`sitemap-flatpages.xml` and :file:`sitemap-blog.xml`. The
|
2013-01-01 13:12:42 +00:00
|
|
|
:class:`~django.contrib.sitemaps.Sitemap` classes and the ``sitemaps``
|
2011-06-28 10:16:34 +00:00
|
|
|
dict don't change at all.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2020-12-30 16:44:53 +00:00
|
|
|
If all sitemaps have a ``lastmod`` returned by
|
|
|
|
:meth:`Sitemap.get_latest_lastmod` the sitemap index will have a
|
|
|
|
``Last-Modified`` header equal to the latest ``lastmod``.
|
|
|
|
|
2008-09-07 22:56:03 +00:00
|
|
|
You should create an index file if one of your sitemaps has more than 50,000
|
|
|
|
URLs. In this case, Django will automatically paginate the sitemap, and the
|
|
|
|
index will reflect that.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2012-02-03 20:45:45 +00:00
|
|
|
If you're not using the vanilla sitemap view -- for example, if it's wrapped
|
2012-01-29 09:30:28 +00:00
|
|
|
with a caching decorator -- you must name your sitemap view and pass
|
|
|
|
``sitemap_url_name`` to the index view::
|
|
|
|
|
|
|
|
from django.contrib.sitemaps import views as sitemaps_views
|
|
|
|
from django.views.decorators.cache import cache_page
|
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
urlpatterns = [
|
2016-10-20 17:29:04 +00:00
|
|
|
path(
|
|
|
|
"sitemap.xml",
|
|
|
|
cache_page(86400)(sitemaps_views.index),
|
|
|
|
{"sitemaps": sitemaps, "sitemap_url_name": "sitemaps"},
|
|
|
|
),
|
|
|
|
path(
|
|
|
|
"sitemap-<section>.xml",
|
|
|
|
cache_page(86400)(sitemaps_views.sitemap),
|
|
|
|
{"sitemaps": sitemaps},
|
|
|
|
name="sitemaps",
|
|
|
|
),
|
2014-04-02 00:46:34 +00:00
|
|
|
]
|
2012-01-29 09:30:28 +00:00
|
|
|
|
2010-12-12 22:56:29 +00:00
|
|
|
Template customization
|
|
|
|
======================
|
|
|
|
|
2011-06-28 10:16:34 +00:00
|
|
|
If you wish to use a different template for each sitemap or sitemap index
|
|
|
|
available on your site, you may specify it by passing a ``template_name``
|
|
|
|
parameter to the ``sitemap`` and ``index`` views via the URLconf::
|
2010-12-12 22:56:29 +00:00
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
from django.contrib.sitemaps import views
|
|
|
|
|
|
|
|
urlpatterns = [
|
2016-10-20 17:29:04 +00:00
|
|
|
path(
|
|
|
|
"custom-sitemap.xml",
|
|
|
|
views.index,
|
2010-12-12 22:56:29 +00:00
|
|
|
{"sitemaps": sitemaps, "template_name": "custom_sitemap.html"},
|
2021-10-21 14:09:29 +00:00
|
|
|
name="django.contrib.sitemaps.views.index",
|
|
|
|
),
|
2016-10-20 17:29:04 +00:00
|
|
|
path(
|
|
|
|
"custom-sitemap-<section>.xml",
|
|
|
|
views.sitemap,
|
2010-12-12 22:56:29 +00:00
|
|
|
{"sitemaps": sitemaps, "template_name": "custom_sitemap.html"},
|
2016-10-03 20:04:30 +00:00
|
|
|
name="django.contrib.sitemaps.views.sitemap",
|
|
|
|
),
|
2014-04-02 00:46:34 +00:00
|
|
|
]
|
2010-12-12 22:56:29 +00:00
|
|
|
|
2011-06-28 10:17:01 +00:00
|
|
|
|
2012-12-26 20:47:29 +00:00
|
|
|
These views return :class:`~django.template.response.TemplateResponse`
|
|
|
|
instances which allow you to easily customize the response data before
|
|
|
|
rendering. For more details, see the :doc:`TemplateResponse documentation
|
|
|
|
</ref/template-response>`.
|
2011-06-28 10:17:01 +00:00
|
|
|
|
2011-06-28 10:16:34 +00:00
|
|
|
Context variables
|
2017-03-20 22:30:32 +00:00
|
|
|
-----------------
|
2011-06-28 10:16:34 +00:00
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
When customizing the templates for the
|
|
|
|
:func:`~django.contrib.sitemaps.views.index` and
|
|
|
|
:func:`~django.contrib.sitemaps.views.sitemap` views, you can rely on the
|
2011-06-28 10:16:34 +00:00
|
|
|
following context variables.
|
|
|
|
|
2020-12-30 16:44:53 +00:00
|
|
|
.. _sitemap-index-context-variables:
|
|
|
|
|
2011-06-28 10:16:34 +00:00
|
|
|
Index
|
|
|
|
-----
|
|
|
|
|
2020-12-30 16:44:53 +00:00
|
|
|
The variable ``sitemaps`` is a list of objects containing the ``location`` and
|
|
|
|
``lastmod`` attribute for each of the sitemaps. Each URL exposes the following
|
|
|
|
attributes:
|
|
|
|
|
|
|
|
- ``location``: The location (url & page) of the sitemap.
|
|
|
|
- ``lastmod``: Populated by the :meth:`~Sitemap.get_latest_lastmod`
|
|
|
|
method for each sitemap.
|
|
|
|
|
2011-06-28 10:16:34 +00:00
|
|
|
Sitemap
|
|
|
|
-------
|
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
The variable ``urlset`` is a list of URLs that should appear in the
|
2011-06-28 10:16:34 +00:00
|
|
|
sitemap. Each URL exposes attributes as defined in the
|
|
|
|
:class:`~django.contrib.sitemaps.Sitemap` class:
|
|
|
|
|
2020-07-29 08:33:20 +00:00
|
|
|
- ``alternates``
|
2011-10-14 00:12:01 +00:00
|
|
|
- ``changefreq``
|
|
|
|
- ``item``
|
|
|
|
- ``lastmod``
|
|
|
|
- ``location``
|
|
|
|
- ``priority``
|
2011-06-28 10:16:34 +00:00
|
|
|
|
2020-07-29 08:33:20 +00:00
|
|
|
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.
|
|
|
|
|
2011-06-28 10:16:34 +00:00
|
|
|
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
|
|
|
|
``publication_data`` and a ``tags`` field something like this would
|
|
|
|
generate a Google News compatible sitemap:
|
|
|
|
|
|
|
|
.. code-block:: xml+django
|
|
|
|
|
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<urlset
|
2017-05-20 15:51:21 +00:00
|
|
|
xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"
|
2017-01-30 17:07:14 +00:00
|
|
|
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
|
2011-06-28 10:16:34 +00:00
|
|
|
{% spaceless %}
|
|
|
|
{% for url in urlset %}
|
|
|
|
<url>
|
|
|
|
<loc>{{ url.location }}</loc>
|
|
|
|
{% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}</lastmod>{% endif %}
|
|
|
|
{% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %}
|
|
|
|
{% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %}
|
|
|
|
<news:news>
|
|
|
|
{% if url.item.publication_date %}<news:publication_date>{{ url.item.publication_date|date:"Y-m-d" }}</news:publication_date>{% endif %}
|
|
|
|
{% if url.item.tags %}<news:keywords>{{ url.item.tags }}</news:keywords>{% endif %}
|
|
|
|
</news:news>
|
|
|
|
</url>
|
|
|
|
{% endfor %}
|
|
|
|
{% endspaceless %}
|
|
|
|
</urlset>
|
|
|
|
|
2021-04-27 11:09:00 +00:00
|
|
|
.. _`Google news sitemaps`: https://support.google.com/news/publisher-center/answer/9606710
|