Skip Navigation

How to dynamically generate sitemap.xml for a Django website

I am assuming that a Django application with the app name as "blog", which serves a blog is already setup, and the website has a Homepage, a privacy policy page and several individual pages for each blog post.
The privacy policy is a static page which serves a template at a static URL
The URL for each blog post (corresponds to each Blog model) is determined by the permalink column of the model/table: Blog

Let us see how to integrate sitemap generator
We will be using a Django provided framework called The Sitemap Framework to make this work.

Step 1: Edit the file

Edit you projects' file and add django.contrib.sitemaps to the INSTALLED_APPS list


Step 2: Create

Create a file named inside your app directory. This will be the same location where you have your file.
Add the below lines to your

from django.contrib.sitemaps import Sitemap
from django.urls import reverse
from .models import *

class Static_Sitemap(Sitemap):

    priority = 1.0
    changefreq = 'yearly'

    def items(self):
        return ['blog:privacypolicy']

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

class Blog_Sitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.8

    def items(self):
        return Blog.objects.filter(active=True).order_by('-publishdate')

    def location(self, obj):
        return f"/{obj.permalink}"

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

We have two classes: one to generate static URLs and another one to generate dynamic ones from the table. You can add as many static URLs you want by adding the corresponding view function to the return list of items(self) function

Step 3: Update to add the sitemap URLs

Add the below content to the app's file

sitemaps = {
    'post': Blog_Sitemap(),
    'static': Static_Sitemap(),

urlpatterns = [
    path('', views.index, name="index"),
path('sitemap.xml',  sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),

All Done! Your sitemap file should be available at the URL now.

Add a comment