Django Sitemap Framework Tutorial

Django Sitemap Framework Tutorial Author: Mohammad Amaan Abbasi | July 20, 2018

Sitemaps are XML files, that serves as a clue to google that you consider the web pages in the XML sitemap to be of good-quality, and worthy of indexation. If you look at a sitemap file its just a list of web pages outlining the organization of your site content. A common misconception is that XML sitemaps helps get your pages indexed.

How a sitemap looks like click here.

Why do you need a Sitemap(s) for your website.

A sitemap can improve the crawling of your website. Also, Sitemaps will communicate your website contents to search engines, So that the search engines have an easier time indexing the contents of your website. However, using a sitemap doesn't guarantee that your web pages will be crawled and indexed, as this process is subjected to complex algorithms. But, in most cases, your website will benefit from a sitemap, and you'll never be penalized for having one.

Django Sitemap framework

Django comes with a high-level sitemap framework that makes creating sitemap XML files easy. In this tutorial, we will create sitemaps to include all the links to individual blog entries.


  • Add django.contrib.sitemaps and django.contrib.sites' to your INSTALLED_APPS setting.

  • You may also want to include SITE_ID as it is necessary to identify a resource in a database. Don't worry too much about it right now, just set it to 1.

Now, let's start creating our sitemap. Create a file called in any of your app. In my case, I put it in a app called blog.

  • line 1, import the Sitemap package.
  • line 2, import a Class from my file.
  • line 3, Create a class called PostSitemap which will be a sitemap for all of our Post objects.
  • line 4, Every sitemap has to have a function called items, which returns all the objects from a class(Post class in this case), and those objects will be included in the sitemaps.xml file.

Now, we have to import the PostSitemap into our main file.

line 3-4, we have imported the PostSitemap class and the sitemap view. Then we have created a path, which takes in a file sitemap.xml, the sitemap view and the dictionary that we created above.

Run the server, and go to localhost:8000/sitemaps.xml, you will see the xml file. In case you get an error, Try running python migrate command in the command prompt.


Add static pages to sitemaps.xml file. Let's start by creating a view.

A simple view function that renders a about.html template.

Now, add this view to the app's blog app in this case).

We are done with the view, It's the time to add this view to the sitemap. Open up the file.

And add the following:

It's the same as the previous except this time we have a function called location that returns the path of the item. We use the reverse method to get the correct path of the object.

Finally, we'll import the StaticViewSitemap into our main file.


Sitemap.xml file contains the list of the web pages of your site, It helps the web crawler to more intelligently crawl your site. We use Django's sitemap framework to create a Sitemap file for a blogging system.

More Resources