Luke Ryan Jernejcic

Django Sitemap

July 08, 2010

In setting up this blog I came up with the problem of setting up a sitemap. I did not want to use a django flatpage because I already a template and use for them, not to mention the amount of work that it would take to maintain. A quick search on Google turned up that django already has some sitemap functionality built right into the contrib! Awesome! Here is the code that was used.

First I created a new app to hold the logic for my site maps: site\_sitemap. The models.py looks like this:

from django.contrib.sitemaps import Sitemap
from django.db import models

from articles.models import *

sitemaps = {
    'articles' : GenericSitemap({
    'queryset': Article.objects.filter(status__name__exact='Finished'),
    'date_field': 'publish_date'},
    changefreq = 'weekly', priority = 0.4)
}

Then in the urls.py I put this:

from site_sitemap.models import sitemaps
....
url(r'^sitemap.xml$',
    'django.contrib.sitemaps.views.sitemap',
    {'sitemaps': sitemaps},
    name='sitemap'),

And that was about it.


Written by Luke Ryan Jernejcic who lives and works in Austin Texas building useful things. Follow him on Twitter