What You'll Learn in This Hour |
|
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 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.
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:
items() is required. The items() method should return a list of objects that will be used to build this section of the sitemap. The items() function also is used to build a list of objects that are passed to the location, lastmod, changefreq, and priority members of the class if they are defined as functions.
Did you Know?
The sitemaps framework doesn't require the objects returned by items() to be objects in the Django model. You could return your own list of objects to customize the sitemap even further.
location is optional. The location member can be defined as an attribute or function. As a function, location should return a string representation of the absolute URL of the object that is passed into it from the items() list. As an attribute, location should be set to the URL for all objects in the items() list. If location is not specified in the class, the framework calls the get_absolute_url() method on the object to obtain the URL.
lastmod is optional. The lastmod member can be defined as an attribute or function. As a function, lastmod should return a Python datetime object representing the last modification date of the object that is passed into it from the items() list. As an attribute, lastmod should be set to the last modified date for all objects in the items() list.
priority is optional. The priority member can be defined as an attribute or function. As a function, priority should return a string or float representing the priority of the object that is passed into it from the items() list. As an attribute, priority should be set to a string or float representing the priority for all objects in the items() list. Valid values are from 0.0 to 1.0. The default value is .5.
changefreq is optional. The changefreq member can be defined as an attribute or function. As a function, changefreq should return a string representing the change frequency of the object that is passed into it from the items() list. As an attribute, changefreq should be set to a string representing the change frequency for all objects in the items() list. Valid strings are always, hourly, daily, weekly, monthly, yearly, and never.
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
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 SitemapIn 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:
Listing 21.1. Full Contents of the iFriends/sitemaps.py File
Listing 21.2. The Sitemap Section of the iFriends/urls.py File
|