Creating Sitemaps in Django

Nov. 12, 2022

2 min read

A sitemap is an XML file that informs search engines such as Google, about the pages of your website, their relevance, and how frequently they are updated. Using sitemaps facilitates crawlers indexing the site, therefore sitemap plays a crucial role in modern SEO (Search Engine Optimization).

Creating Sitemaps in Django

Considering the scope of the article I am assuming you have basic knowledge of Django and have a project up and running if not read the following article first.


Add 'django.contrib.sitemaps' in INSTALLED_APPS

# config/
    'django.contrib.sites', # new
    'django.contrib.sitemaps', # new 


SITE_ID = 1 # new

 Make sure your TEMPLATES setting contains a DjangoTemplates backend whose APP_DIRS options are set to True. It's in there by default, revert it in case you have changed it.

Creating Sitemap

I am generating a sitemap for a blog application following is the file for reference.

class Post(models.Model):
    title = models.CharField(max_length=200, unique=True)
    slug = models.SlugField(max_length=200, unique=True)
    author = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name="blog_posts"
    updated_on = models.DateTimeField(auto_now=True)
    content = models.TextField()
    created_on = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ["-created_on"]

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('read-more', kwargs={'slug': self.slug})


There are two URL files to update: our top-level one at projects/ and then one within the recipes app that must be created. Let's create that new one now.

from django.contrib import admin
from django.urls import path, include
from django.contrib.sitemaps import GenericSitemap  # for site map
from django.contrib.sitemaps.views import sitemap  # for site map

info_dict = {
    'queryset': Blog.objects.all(),

urlpatterns = [
    path('', include('blog.urls')),
    path('sitemap.xml', sitemap,  # for site map
         {'sitemaps': {'blog': GenericSitemap(info_dict, priority=1)}},

Make sure the server is running and visit

There's our sitemap! But notice that it's used as the name of our site. This comes from the sites framework. If you go into the Django admin, there's a new section called Sites where you can change the name.

It's also possible to change the priority and changefreq. And while you might notice that the current version uses http rather than https, if your production site uses https then that will be reflected in the sitemap. Or you can change the protocol entirely, if so desired.


django Python sitemaps Appreciate you stopping by my post! 😊

Add a comment

Note: If you use these tags, write your text inside the HTML tag.
Login Required
Author's profile
Profile Image

Abdulla Fajal

Django Developer

With '' under my care, I, Abdulla Fajal, graciously invite your insights and suggestions, as we endeavour to craft an exquisite online experience together.