How to Create a Dynamic Sitemap in Django

Aug. 31, 2020, 5:13 p.m.

Django Production SEO · 5 min read

How to Create a Dynamic Sitemap in Django

A sitemap is an XML file on your website that search engines read to intelligently crawl your site. It not only provides information about the pages and files you find important, but it also states when the page was last updated and how often the page is changed.

If you are using Django, create your sitemap automatically with the sites and sitemap frameworks.


Install the sites app

env > mysite > mysite >

    'django.contrib.sites',    #add sites to installed apps

SITE_ID = 1   #define the site id

First, install the sites app. Then define the SITE_ID in the settings. The sites framework will be used to define the name of the site in the sitemap. 


Run migrations

macOS Terminal

(env)User-Macbook:mysite user$ python3 migrate

Windows Command Prompt

(env)C:\Users\Owner\desktop\env\mysite> py migrate

Run migrate in the command prompt to add the sites table to the database.


Create a superuser

macOS Terminal

(env)User-Macbook:mysite user$ python3 createsuperuser
Username (leave blank to use 'owner'): admin
Email address:
Password: *****
Password (again): *****
Superuser created successfully.

(env)User-Macbook:mysite user$ python3 runserver

Windows Command Prompt

(env) C:\Users\Owner\Desktop\Code\env\mysite>py createsuperuser
Username (leave blank to use 'owner'): admin
Email address: 
Password: *****
Password (again): *****
Superuser created successfully.

(env) C:\Users\Owner\Desktop\Code\env\mysite>py runserver

If you haven't already, create a superuser so you can access the Django administration built-in your project.


Log in to the Django administration

django.contrib.sites registers a signal handler that creates a default site named You will need to correct the name and domain of your Django project by logging in to the Django admin at

Find the link Sites and click it.

Site link in the django admin

You will then see a site domain name and display name listed, both of which should be changed to the appropriate website name.

For development, use the localhost For production, change the names to your actual domain name. 

Site listed in Django admin


Install the sitemap app

env > mysite > mysite >

    'django.contrib.sitemaps',   #add sitemaps to installed apps

        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [

Now that we have defined the name of the site, we can use the sitemap framework.

To install the sitemap app, add 'django.contrib.sitemaps' to INSTALLED_APPS. Then scroll down to TEMPLATES making sure DjangoTemplates is listed (it should be there by default) and the APP_DIRS is set to True.


Create a sitemap file

env > mysite > main > (New File)

from django.contrib.sitemaps import Sitemap
from .models import Article
class ArticleSitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.8
    protocol = 'http'

    def items(self):
        return Article.objects.all()

    def lastmod(self, obj):
        return obj.article_published
    def location(self,obj):
        return '/blog/%s' % (obj.article_slug)

Creating a sitemap is similar to creating a Django model. First, create a new file called in your app's folder. This is the folder with and

Then import the Sitemap class from django.contrib.sitemaps at the top of the file. Also import the model containing all of the articles/pages you want on the sitemap, in this case, the model Article.

Next, create a sitemap class named ArticleSitemap that extends Sitemap class.

We can then add the attributes changefreq, priority, and protocol

changefreq is how frequently the content on the page changes. The value can be:

  • 'always'
  • 'hourly'
  • 'daily'
  • 'weekly'
  • 'monthly'
  • 'yearly'
  • 'never'

priority is the importance of the pages to the other pages on your site. The value can be anywhere from 0.0 to 1.0

protocol is either 'https' or 'http'. In this case, we will use HTTP given the localhost domain is

The items() method returns a list of all of the model objects we want to be listed in the sitemap.

The lastmod() method returns the last time the model object was modified, meaning you need to return a defined publication date field specified in the model for it to work properly.

Finally, the location() method returns the URL location of the article. By default, the sitemap framework calls get_absolute_url().

So if your article URLs is anything other than, you need to add the location() method and return the appropriate slugs listed before the article slug.

As per the example above, the URL location is now


Add the sitemap URL

env > mysite > main >

from django.urls import path
from . import views
from django.contrib.sitemaps.views import sitemap
from .sitemaps import ArticleSitemap

app_name = "main"

sitemaps = {

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


The last thing that needs to be done to view your sitemap is to add its URL to the list of app URLs. Import sitemap and ArticleSitemap from the appropriate files. Then add a sitemap dictionary that maps to the sitemap class ArticleSitemap. Finally, add the URL pattern that points to the sitemap. 


View the sitemap

The sitemap is now available at When you go to this domain, it should list all of the URLs defined in the Article model along with the attributes added in


Subscribe now

Subscribe to stay current on our latest articles and promos

Post a Comment
Join the community