Previous Page Next Page

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


Try It Yourself: Create and Enable a Sitemap for Generic Views

In this section, you will create and enable a GenericSitemap class that will generate a sitemap for the blog_details generic view you created in Hour 12, "Utilizing Generic Views." Follow these steps to create and enable the sitemap:

1.
Open the iFriends/urls.py file.

2.
Add the following import statement, shown in Listing 21.3, to the beginning of the file to import the GenericSitemap class:

from django.contrib.sitemaps import GenericSitemap

3.
Add the following line to the blog_detail_info dictionary, shown in Listing 21.3, so that the GenericSitemap includes the date field as the last modified field in the sitemap:

'date_field' : 'date',

4.
Add the following entry to the sitemaps dictionary, shown in Listing 21.3, to add the GenericSitemap class for the generic blog_details view to the sitemap:

'blog': GenericSitemap(blog_detail_info, priority=0.3, changefreq='weekly'),


					  

5.
Save the iFriends/urls.py file.

6.
Open the iFriends/People/models.py file.

7.
Add the following get_absolute_url() function to the Blog model, as shown in Listing 21.4, so that the GenericSitemap can fill in the location attribute automatically for each object in the queryset:

def get_absolute_url(self):
    return '/generic/blog_details/%d' % self.id

8.
Save the iFriends/People/models.py file.

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

http://127.0.0.1:8000/sitemap.xml

Figure 21.2. The sitemap.xml document of the iFriends website, showing additional entries from the blog_details generic view.


Listing 21.3. The Imports, blog_detail_info Dictionary, and Sitemap Sections of the iFriends/urls.py File

from django.contrib.sitemaps import GenericSitemap
. . .
blog_detail_info = {
    'queryset' : Blog.objects.all(),
    'date_field' : 'date',
    'template_object_name': 'blog',
    'template_name' : 'Blogs/blog_detail.html',
}
. . .
sitemaps = {
    'person': PersonSitemap,
    'blog': GenericSitemap(blog_detail_info, priority=0.3,
                           changefreq='weekly'),
}
urlpatterns += patterns('',
    (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap',
                        {'sitemaps': sitemaps}),
)

Listing 21.4. The Blog Class Definition in the iFriends/People/models.py File

class Blog(models.Model):
    title = models.CharField('Title', max_length=200)
    text = models.TextField('Text', max_length=2048)
    date = models.DateTimeField('Last Modified')

    def __str__(self):
        return '%s' % (self.title)

    def blog_size(self):
        return len(self.text)

    def get_absolute_url(self):
        return '/generic/blog_details/%d' % self.id

    class Admin:
        list_display = ('title', 'date', 'blog_size',)
        list_filter = ('date',)
        date_hierarchy = 'date'
        search_fields = ('title', 'text',)

    class Meta:
        permissions = (
            ('can_blog', 'Allowed to Blog'),
        )


Previous Page Next Page