Creating a sitemap index in Django

(Comments)

Sitemaps are simple XML files that are used to provide details for every URL on a website. They include location, date of last modification, change frequency and page priority. If you’ve got an international or multilingual site, you can also use your sitemap to note the relationship between language versions of a URL. This helps search engines to learn about the site’s structure and index your website.

Django has built in sitemap generation framework which makes this task easy. We will see how to generate sitemaps containing dynamic and static urls.

Make sure that you have the following apps in your INSTALLED_APPS in your settings.py file

INSTALLED_APPS = (
    ...
    'django.contrib.sites',
    'django.contrib.sitemaps',
    ...
)

Now oprn the urls.py file and add the following lines of code.

from django.contrib.sitemaps.views import sitemap

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

Django will build the sitemap whenever /sitemap.xml is accessed. If you look at the url above, there is an argument called sitemaps. This is a dictionary which maps labels with its Sitemap class.

Let's see how to generate a sitemap for a collection of dynamic and static urls. Static urls are defined in a separate app named “music” and the dynamic ones are generated based on “Musician” model.

Generating Sitemap for Dynamic Urls

Let's say we have number of musicians in your database and we need the sitemap to inculde all the urls to the musicians. First create a sitemaps.py file in “music” app with the following content. This file can reside anywhere in your codebase. For this example, I am defining it in the “music” app. Here’s how the Sitemap class will look like

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

class MusicianSitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.7

    def items(self):
       return Musician.objects.filter(isActive=True)

    def lastmod(self, obj): 
       return obj.modifiedDate

Note that changefreq and priority are class attributes, but they can also be defined as functions like lastmod in the above example.

We have also defined the items method which return all the active musicians.

The Musician model for this example is shown below

class Musician(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    instrument = models.CharField(max_length=100)

    def __unicode__(self):
        return self.first_name

    def get_absolute_url(self):
        return '/'+self.first_name+'-'+self.instrument+'/'

Now let's add the code for generating sitemap for static urls to the already created sitemaps.py file.

Generating Sitemap for Static Urls

from music.urls import urlpatterns as musicUrls
from django.core.urlresolvers import reverse

class StaticSitemap(Sitemap):
     priority = 0.8
     changefreq = 'weekly'

     # The below method returns all urls defined in urls.py file
     def items(self):
        mylist = [ ]
        for url in musicUrls:
            mylist.append('music:'+url.name) 
        return mylist

     def location(self, item):
         return reverse(item)

We have the code for generating sitemap for static and dynamic urls. All we have to do now is to update the sitemap url in the urls.py file that we added in the begining. Update the file as shown below.

from django.contrib.sitemaps.views import sitemap
from music.sitemaps import *

    # Dictionary containing your sitemap classes
    sitemaps = {
       'musicians': MusicianSitemap(),
       'static': StaticSitemap(),
    }

    urlpatterns = [    ....,
        url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
        ....,
      ]

Run the /sitemap.xml in your browser and you will see the generated xml sitemap file.

Let me know if you have any suggestions/ feedback in the comments section below.

Comments

Recent Posts

Archive

2022
2021
2020
2019
2018
2017
2016
2015
2014

Tags

Authors

Feeds

RSS / Atom