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 settings.py file

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

INSTALLED_APPS = [
    'blog',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sitemaps',
]

Step 2: Create sitemaps.py

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

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 urls.py to add the sitemap URLs

Add the below content to the app's urls.py 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 http://127.0.0.1:8000/sitemap.xml now.

Add a comment