5 Django comes with a high-level sitemap-generating framework that makes
6 creating sitemap_ XML files easy.
8 .. _sitemap: http://www.sitemaps.org/
13 A sitemap is an XML file on your Web site that tells search-engine indexers how
14 frequently your pages change and how "important" certain pages are in relation
15 to other pages on your site. This information helps search engines index your
18 The Django sitemap framework automates the creation of this XML file by letting
19 you express this information in Python code.
21 It works much like Django's `syndication framework`_. To create a sitemap, just
22 write a ``Sitemap`` class and point to it in your URLconf_.
24 .. _syndication framework: ../syndication_feeds/
25 .. _URLconf: ../url_dispatch/
30 To install the sitemap app, follow these steps:
32 1. Add ``'django.contrib.sitemaps'`` to your INSTALLED_APPS_ setting.
33 2. Make sure ``'django.template.loaders.app_directories.load_template_source'``
34 is in your TEMPLATE_LOADERS_ setting. It's in there by default, so
35 you'll only need to change this if you've changed that setting.
36 3. Make sure you've installed the `sites framework`_.
38 (Note: The sitemap application doesn't install any database tables. The only
39 reason it needs to go into ``INSTALLED_APPS`` is so that the
40 ``load_template_source`` template loader can find the default templates.)
42 .. _INSTALLED_APPS: ../settings/#installed-apps
43 .. _TEMPLATE_LOADERS: ../settings/#template-loaders
44 .. _sites framework: ../sites/
49 To activate sitemap generation on your Django site, add this line to your
52 (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})
54 This tells Django to build a sitemap when a client accesses ``/sitemap.xml``.
56 The name of the sitemap file is not important, but the location is. Search
57 engines will only index links in your sitemap for the current URL level and
58 below. For instance, if ``sitemap.xml`` lives in your root directory, it may
59 reference any URL in your site. However, if your sitemap lives at
60 ``/content/sitemap.xml``, it may only reference URLs that begin with
63 The sitemap view takes an extra, required argument: ``{'sitemaps': sitemaps}``.
64 ``sitemaps`` should be a dictionary that maps a short section label (e.g.,
65 ``blog`` or ``news``) to its ``Sitemap`` class (e.g., ``BlogSitemap`` or
66 ``NewsSitemap``). It may also map to an *instance* of a ``Sitemap`` class
67 (e.g., ``BlogSitemap(some_var)``).
69 .. _URLconf: ../url_dispatch/
74 A ``Sitemap`` class is a simple Python class that represents a "section" of
75 entries in your sitemap. For example, one ``Sitemap`` class could represent all
76 the entries of your weblog, while another could represent all of the events in
79 In the simplest case, all these sections get lumped together into one
80 ``sitemap.xml``, but it's also possible to use the framework to generate a
81 sitemap index that references individual sitemap files, one per section. (See
82 `Creating a sitemap index`_ below.)
84 ``Sitemap`` classes must subclass ``django.contrib.sitemaps.Sitemap``. They can
85 live anywhere in your codebase.
90 Let's assume you have a blog system, with an ``Entry`` model, and you want your
91 sitemap to include all the links to your individual blog entries. Here's how
92 your sitemap class might look::
94 from django.contrib.sitemaps import Sitemap
95 from mysite.blog.models import Entry
97 class BlogSitemap(Sitemap):
102 return Entry.objects.filter(is_draft=False)
104 def lastmod(self, obj):
109 * ``changefreq`` and ``priority`` are class attributes corresponding to
110 ``<changefreq>`` and ``<priority>`` elements, respectively. They can be
111 made callable as functions, as ``lastmod`` was in the example.
112 * ``items()`` is simply a method that returns a list of objects. The objects
113 returned will get passed to any callable methods corresponding to a
114 sitemap property (``location``, ``lastmod``, ``changefreq``, and
116 * ``lastmod`` should return a Python ``datetime`` object.
117 * There is no ``location`` method in this example, but you can provide it
118 in order to specify the URL for your object. By default, ``location()``
119 calls ``get_absolute_url()`` on each object and returns the result.
121 Sitemap class reference
122 =======================
124 A ``Sitemap`` class can define the following methods/attributes:
129 **Required.** A method that returns a list of objects. The framework doesn't
130 care what *type* of objects they are; all that matters is that these objects
131 get passed to the ``location()``, ``lastmod()``, ``changefreq()`` and
132 ``priority()`` methods.
137 **Optional.** Either a method or attribute.
139 If it's a method, it should return the absolute URL for a given object as
140 returned by ``items()``.
142 If it's an attribute, its value should be a string representing an absolute URL
143 to use for *every* object returned by ``items()``.
145 In both cases, "absolute URL" means a URL that doesn't include the protocol or
148 * Good: ``'/foo/bar/'``
149 * Bad: ``'example.com/foo/bar/'``
150 * Bad: ``'http://example.com/foo/bar/'``
152 If ``location`` isn't provided, the framework will call the
153 ``get_absolute_url()`` method on each object as returned by ``items()``.
158 **Optional.** Either a method or attribute.
160 If it's a method, it should take one argument -- an object as returned by
161 ``items()`` -- and return that object's last-modified date/time, as a Python
162 ``datetime.datetime`` object.
164 If it's an attribute, its value should be a Python ``datetime.datetime`` object
165 representing the last-modified date/time for *every* object returned by
171 **Optional.** Either a method or attribute.
173 If it's a method, it should take one argument -- an object as returned by
174 ``items()`` -- and return that object's change frequency, as a Python string.
176 If it's an attribute, its value should be a string representing the change
177 frequency of *every* object returned by ``items()``.
179 Possible values for ``changefreq``, whether you use a method or attribute, are:
192 **Optional.** Either a method or attribute.
194 If it's a method, it should take one argument -- an object as returned by
195 ``items()`` -- and return that object's priority, as either a string or float.
197 If it's an attribute, its value should be either a string or float representing
198 the priority of *every* object returned by ``items()``.
200 Example values for ``priority``: ``0.4``, ``1.0``. The default priority of a
201 page is ``0.5``. See the `sitemaps.org documentation`_ for more.
203 .. _sitemaps.org documentation: http://www.sitemaps.org/protocol.html#prioritydef
208 The sitemap framework provides a couple convenience classes for common cases:
213 The ``django.contrib.sitemaps.FlatPageSitemap`` class looks at all flatpages_
214 defined for the current ``SITE_ID`` (see the `sites documentation`_) and
215 creates an entry in the sitemap. These entries include only the ``location``
216 attribute -- not ``lastmod``, ``changefreq`` or ``priority``.
218 .. _flatpages: ../flatpages/
219 .. _sites documentation: ../sites/
224 The ``GenericSitemap`` class works with any `generic views`_ you already have.
225 To use it, create an instance, passing in the same ``info_dict`` you pass to
226 the generic views. The only requirement is that the dictionary have a
227 ``queryset`` entry. It may also have a ``date_field`` entry that specifies a
228 date field for objects retrieved from the ``queryset``. This will be used for
229 the ``lastmod`` attribute in the generated sitemap. You may also pass
230 ``priority`` and ``changefreq`` keyword arguments to the ``GenericSitemap``
231 constructor to specify these attributes for all URLs.
233 .. _generic views: ../generic_views/
238 Here's an example of a URLconf_ using both::
240 from django.conf.urls.defaults import *
241 from django.contrib.sitemaps import FlatPageSitemap, GenericSitemap
242 from mysite.blog.models import Entry
245 'queryset': Entry.objects.all(),
246 'date_field': 'pub_date',
250 'flatpages': FlatPageSitemap,
251 'blog': GenericSitemap(info_dict, priority=0.6),
254 urlpatterns = patterns('',
255 # some generic view using info_dict
259 (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})
262 .. _URLconf: ../url_dispatch/
264 Creating a sitemap index
265 ========================
267 The sitemap framework also has the ability to create a sitemap index that
268 references individual sitemap files, one per each section defined in your
269 ``sitemaps`` dictionary. The only differences in usage are:
271 * You use two views in your URLconf: ``django.contrib.sitemaps.views.index``
272 and ``django.contrib.sitemaps.views.sitemap``.
273 * The ``django.contrib.sitemaps.views.sitemap`` view should take a
274 ``section`` keyword argument.
276 Here is what the relevant URLconf lines would look like for the example above::
278 (r'^sitemap.xml$', 'django.contrib.sitemaps.views.index', {'sitemaps': sitemaps})
279 (r'^sitemap-(?P<section>.+).xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})
281 This will automatically generate a ``sitemap.xml`` file that references
282 both ``sitemap-flatpages.xml`` and ``sitemap-blog.xml``. The ``Sitemap``
283 classes and the ``sitemaps`` dict don't change at all.
288 You may want to "ping" Google when your sitemap changes, to let it know to
289 reindex your site. The framework provides a function to do just that:
290 ``django.contrib.sitemaps.ping_google()``.
292 ``ping_google()`` takes an optional argument, ``sitemap_url``, which should be
293 the absolute URL of your site's sitemap (e.g., ``'/sitemap.xml'``). If this
294 argument isn't provided, ``ping_google()`` will attempt to figure out your
295 sitemap by performing a reverse looking in your URLconf.
297 ``ping_google()`` raises the exception
298 ``django.contrib.sitemaps.SitemapNotFound`` if it cannot determine your sitemap
301 One useful way to call ``ping_google()`` is from a model's ``save()`` method::
303 from django.contrib.sitemaps import ping_google
305 class Entry(models.Model):
308 super(Entry, self).save()
312 # Bare 'except' because we could get a variety
313 # of HTTP-related exceptions.
316 A more efficient solution, however, would be to call ``ping_google()`` from a
317 cron script, or some other scheduled task. The function makes an HTTP request
318 to Google's servers, so you may not want to introduce that network overhead
319 each time you call ``save()``.