Previous Page Next Page

Creating a Sitemap Index

As you add more and more Sitemap classes to your sitemap, you may find the need to index them individually. Django provides a simple solution for indexing the Sitemap classes.

The django.contrib.sitemaps.views.sitemap view function accepts a section argument that is the string value of one of the keys in the sitemaps dictionary.

For example, the following code snippet defines a sitemap with two Sitemap classes, report and log:

sitemaps = {
    'report': ReportSitemap,
    'log': GenericSitemap(log_info_dict, priority=0.2, changefreq='daily'),
}

The following URL pattern displays sitemaps for both the /sitemap-report.xml and /sitemap-log.xml URLs:

(r'^sitemap-(?P<section>.+).xml$', 'django.contrib.sitemaps.views.sitemap',
                   {'sitemaps': sitemaps}),

Try It Yourself: Implement a Sitemap Index

In this section, you will add a sitemap index that will index both the person and blog sitemaps that you created in the previous sections. Follow these steps to implement the sitemap index:

1.
Open the iFriends/urls.py file.

2.
Add the following pattern to the URL patterns for sitemaps, as shown in Listing 21.5, to implement a sitemap index that allows URL patterns that include the Sitemap class key name:

(r'^sitemap-(?P<section>.+).xml$', 'django.contrib.sitemaps.views.sitemap',
               {'sitemaps': sitemaps}),

3.
Open the following URL in a browser to view the sitemap of the PersonSitemap class, as shown in Figure 21.3:

http://127.0.0.1:8000/sitemap-person.xml

Figure 21.3. The sitemap-person.xml document of the iFriends website, showing the entries from the Person details view.


4.
Open the following URL in a browser to view the sitemap with the entries from the blog_details view, as shown in Figure 21.4:

http://127.0.0.1:8000/sitemap-blog.xml



Figure 21.4. The sitemap-blog.xml document of the iFriends website, showing the entries from the Blog blog_details view.


Listing 21.5. The URL Patterns Section for Sitemaps in the iFriends/urls.py File

urlpatterns += patterns('',
    (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap',
                        {'sitemaps': sitemaps}),
    (r'^sitemap-(?P<section>.+).xml$',
   'django.contrib.sitemaps.views.sitemap',
                        {'sitemaps': sitemaps}),
)


Previous Page Next Page