matterkkila.com/

code monkey

django sitemaps. why? because google loves them

April 9, 2008 - 7:14 PM

Sitemaps are an important part for any site. Why? Google loves them, thats why. And you don't want to anger "The Google" do you?

Django knows this and makes them easy to do, especially if you use generic views. You only need to do a few simple things.

1. Add django.contrib.sitemaps to your INSTALLED_APPS.
2. Add django.template.loaders.app_directories.load_template_source to your TEMPLATE_LOADERS.
3. Create a set of dictionary objects and send them to the generic sitemap view.

Check out the Django Sitemaps page for more detailed info.


Example Code to build the sitemap for this blog:

from django.conf.urls.defaults import *
from django.contrib.sitemaps import FlatPageSitemap, GenericSitemap

from blog.models import Post, Category

post_info = {
  'queryset' : Post.live.all(),
  'date_field' : 'pub_date',
  'month_format' : '%m',
}

category_info = {
  'queryset': Category.objects.all(),
}

sitemaps = {
  'blog' : GenericSitemap(post_info, priority=0.7),
  'categories' : GenericSitemap(category_info, priority=0.4, changefreq='weekly'),
}

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

Posted In: