Chapter 21. Creating Sitemaps

<feature><title>What You’ll Learn in This Hour</title> <objective>

How to create and enable a sitemap

</objective>
<objective>

How to create a sitemap for generic views

</objective>
<objective>

How to create a sitemap index

</objective>
<objective>

How to notify Google that your website content has changed

</objective>
</feature>

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:

  • 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

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/.

Creating a Sitemap of Generic Views

Django provides a useful shortcut that allows you to create a sitemap for generic views without having to define your own Sitemap class. Instead, you can use the django.contrib.sitemaps.GenericSitemap class.

The GenericSitemap class builds a Sitemap class from the same info dictionary that you pass the generic view. Instead of defining a list() function, the info dictionary needs to contain a queryset entry containing the list of objects. Instead of lastmod, GenericSitemap uses the date_field entry if one is defined in the dictionary. The priority and changefreq values can be specified as arguments when you create an instance of GenericSitemap.

The following is an example of using the GenericSitemap class to build a sitemap for a generic view:

from django.contrib.sitemaps import GenericSitemap
from django.views.generic import date_based
log_info_dict = {
    'queryset' : log.objects.all(),
    'date_field' : 'last_modified',
}
sitemaps = {
    'log': GenericSitemap(log_info_dict, priority=0.2, changefreq='daily'),
}
urlpatterns += patterns('',
    (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap',
    {'sitemaps': sitemaps}),
    (r'^generic/log_arch/$',date_based.archive_index, log_info_dict),
)

Did you Know?

You can also create sitemaps for flatpages on your website. The location is the only attribute attached to the object in the sitemap. The lastmod, changefreq, and priority attributes are not added. To add flatpages to the sitemap, simply add the django.contrib.sitemaps.FlatPageSitemap class to the sitemaps dictionary:

from django.contrib.sitemaps import FlatPageSitemap
sitemaps = { 'flat': FlatPageSitemap, }

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}),

Pinging Google

Django also provides a simple way to ping Google to notify it that your web content has changed and that your site should be reindexed. The django.contrib.sitemaps.ping_google() function tells Google to reindex your site’s sitemap.

The default value for the sitemap is '/sitemap.xml'. However, you can specify a different value by using the sitemap_url argument. For example:

ping_google(sitemap_url='/sitemap-report.xml')

By the Way

The ping_google() function raises the django.contrib.sitemaps.SitemapNotFound exception if it cannot determine the website.

Using the ping_google() function, you can notify Google every time the data in your database changes. The following example pings Google every time the report object is saved:

from django.contrib.sitemaps import ping_google
class report(models.Model):
    . . .
    def save(self):
        super(report, self).save()
        try:
            ping_google(sitemap_url='/sitemap-report.xml')
        except Exception:
            pass

Watch Out!

If objects are being saved frequently, you may not want to add the extra cycles that the ping_google() function generates. Pick objects that do not get updated as frequently, or add a timed function that runs periodically to ping Google.

Summary

In this hour, you learned how to create Sitemap classes of the different views on your website. You learned how to enable the sitemaps so that a sitemap.xml document is created. You also learned how to create an index for sitemaps and how to ping Google to notify it that the site has changed.

Q&A

Q.

Is there a way to ping another search site besides Google?

A.

Not built into Django. However, nothing is stopping you from implementing your own Python ping application and then importing it into your applications.

Workshop

The workshop consists of a set of questions and answers designed to solidify your understanding of the material covered in this hour. Try answering the questions before looking at the answers.

Quiz

1.

What type of sitemap class would you use to build sitemaps for generic views?

2.

What argument can you pass to the django.contrib.sitemaps.views.sitemap view in the URL pattern to build a sitemap for a specific Sitemap class?

3.

What attribute would you add to the Sitemap class to set the importance of the data in that class compared to other Sitemap classes on your own website?

Quiz Answers

1.

The GenericSitemap class

2.

The section argument

3.

The priority attribute

Exercises

1.

If you haven’t already created a view to display the iFriends.models.Quote objects, create one. It can be a generic object_details view or your own view function.

2.

Add a Sitemap class for the iFriends.models.Quote objects that includes the location of the view you created in Exercise 1.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.182.105