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.
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',
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.
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}), )
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/.
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), )
You can also create sitemaps for flatpage
s 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 flatpage
s to the sitemap, simply add the django.contrib.sitemaps.FlatPageSitemap
class to the sitemaps
dictionary:
from django.contrib.sitemaps import FlatPageSitemap sitemaps = { 'flat': FlatPageSitemap, }
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}),
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')
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
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.
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.
1. | If you haven’t already created a view to display the |
2. | Add a |
18.222.182.105