Previous Page Next Page

Hour 21. Creating Sitemaps

What You'll Learn in This Hour

  • How to create and enable a sitemap

  • How to create a sitemap for generic views

  • How to create a sitemap index

  • How to notify Google that your website content has changed

Sitemaps are a great way for web developers to inform search engines about pages on their websites.

By the Way

Search engines that use a crawling method to index content can use sitemaps to determine information about the URLs on the website. (For more information about sitemaps, see www.sitemaps.org.)


A sitemap is an XML file that includes the URLs in a website. It also includes information about each web page, such as the last update date, change frequency, and relative importance to other pages on the website.

Django provides the django.contrib.sitemaps framework, which allows you to quickly and easily create sitemaps for web crawlers. You install the sitemaps and sites frameworks by adding the following lines to the INSTALLED_APPS setting in the settings.py file:

'django.contrib.sites',
'django.contrib.sitemaps',

By the Way

You also need to make certain that the 'django.template.loaders.app_directories.load_template_source', line is included in the TEMPLATE_LOADERS setting of the settings.py file. It should be there by default.


The following sections describe how to create sitemaps for your website views as well as for generic views. They also discuss how to create an index for sitemaps and how to ping Google to notify it that the site has changed.

Creating a Sitemap

Creating a sitemap is a simple two-step process. First you create sitemap classes that define the URLs to include in the sitemap, and then you enable the sitemap in the URLconf file. The following sections discuss this process.

Creating a Sitemap Class

Django uses classes to define sitemaps. Each class includes information about sections of entries in the sitemap. For example, you could use one class to define the blog pages that are available at the website.

Sitemap classes extend the django.contrib.sitemaps.Sitemap object and include the following attributes and methods:

The following code snippet shows an example of a sitemap class definition:

from django.contrib.sitemaps import Sitemap
from mySite.data.models import report

class ReportSitemap(Sitemap):
    changefreq = "yearly"
    priority = 0.7
    def items(self):
        return report.objects.all()
    def location(self, obj):
        return '/datea/report/%d' % obj.id

Enabling the Sitemap

After you have defined your Sitemap classes, you can make them run by adding an entry for the django.contrib.sitemaps.views.sitemap view to the URLconf file. The sitemap view requires an extra argument called sitemaps. The sitemaps argument should be a dictionary that contains entries for each Sitemap class that you want to include in the sitemap.

For example, to enable the class defined in the preceding section, you would need to add the following code to the URLconf file:

sitemaps = { 'report': ReportSitemap, }
urlpatterns += patterns('',
    (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap',
          {'sitemaps': sitemaps}),
)

Did you Know?

Search engines search websites only at the level of the sitemap and below. If you want the search engine to search the entire site, define the sitemap URL as /sitemap.xml. If you want the search engine to search only a specific directory and below, define the sitemap as /directory/sitemap.xml/.


Try It Yourself: Create and Enable a Sitemap

In this section, you will install the sitemaps application and then use it to create and enable a Sitemap class. This class will build a sitemap for the web pages of all Person objects in the iFriends database.

Follow these steps to create and enable the sitemap:

1.
Stop the development server.

2.
Open the iFriends/settings.py file in an editor.

3.
Add the following lines to the INSTALLED_APPS setting if they are not already there:

'django.contrib.sites',
'django.contrib.sitemaps',

4.
Save the iFriends/settings.py file.

5.
Create and open a file called iFriends/sitemaps.py in an editor.

6.
Add the following imports, shown in Listing 21.1, to import the Sitemap and Person classes:

from django.contrib.sitemaps import Sitemap
from iFriends.People.models import Person

7.
Add the following class definition, shown in Listing 21.1, to define a Sitemap class named PersonSitemap and to set the changefreq and priority values for all objects in the class:

class PersonSitemap(Sitemap):
    changefreq = "Yearly"
    priority = 0.7

8.
Add the following items() function, shown in Listing 21.1, to set the Sitemap class to pick up all objects in the Person table:

def items(self):
    return Person.objects.all()

9.
Add the following location() function, shown in Listing 21.1, so that the Sitemap class returns the /Person/info/ URL with the object id appended:

def location(self, obj):
    return '/Person/info/%d' % obj.id

10.
Save the iFriends/sitemaps.py file.

11.
Open the iFriends/urls.py file in an editor.

12.
Add the following line of code, shown in Listing 21.2, to import the PersonSitemap class you just created:

from iFriends.sitemaps import PersonSitemap

13.
Add the following lines of code, shown in Listing 21.2, to create the sitemap dictionary that includes the PersonSitemap class:

sitemaps = {
    'person': PersonSitemap,
}

14.
Add the following lines of code, shown in Listing 21.2, to extend the URL patterns in the URLconf file to enable the sitemap URL:

urlpatterns += patterns('',
    (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap',
                        {'sitemaps': sitemaps}),
)

15.
Save the iFriends/urls.py file.

16.
Start the development server.

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

http://127.0.0.1:8000/sitemap.xml

Figure 21.1. The sitemap.xml document of the iFriends website generated from the PersonSitemap class.


Listing 21.1. Full Contents of the iFriends/sitemaps.py File

from django.contrib.sitemaps import Sitemap
from iFriends.People.models import Person

class PersonSitemap(Sitemap):
    changefreq = "Yearly"
    priority = 0.7
    def items(self):
        return Person.objects.all()

    def location(self, obj):
        return '/Person/info/%d' % obj.id

Listing 21.2. The Sitemap Section of the iFriends/urls.py File

from iFriends.sitemaps import PersonSitemap

sitemaps = {
    'person': PersonSitemap,
}
urlpatterns += patterns('',
    (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap',
                        {'sitemaps': sitemaps}),
)


Previous Page Next Page