Python Django Framework Sitemap Installation


Posted by indranil on Feb. 9, 2021, 10:06 a.m.

How To Add Sitemap On Your Django Project?

A sitemap is an XML file on your website that tells search-engine index. A vital part of modern SEO (Search Engine Optimization) is to have a sitemap XML file that tells a search engine how often a page is updated and how important it is in relation to other pages on the site.

The Django sitemap framework automates the creation of this XML file by letting you express this information in Python code.

A sitemap exists at a single location as a sitemap.xml file such as It works much like Django’s syndication framework. To create a sitemap, write a Sitemap class
and point to it in your URLconf.

django sitemap tutorial

After setup on your django project follow this step

#Add On your Istalled Apps On
#Add Exact Path On Your Models In
#Create Your function on
#Call Your Function By

django sitemap framework

#Add On your Istalled Apps On

django sitemap model

#Add This Function And Make Exact Path

def get_absolute_url(self):
        return '/' + self.url

#Add Exact Path On Your Models In
#Demo Is Here

class Post(models.Model):
    url = models.CharField(max_length=300, default='notset')
    title = models.CharField(max_length=130)
    author = models.CharField(max_length=50)
    post_lng = models.CharField(max_length=100)
    content = models.TextField()
    cover_img = models.ImageField(blank=True)
    tag = models.TextField(default='notset')
    footer_img = models.ImageField(blank=True)
    add_history = models.TextField(default='notset')
    view = models.IntegerField(default=0)
    time_stamp = models.DateTimeField(default=now)

    #Demo Like That
    def get_absolute_url(self):

        return '/' + self.url
    def __str__(self):
        return self.title + 'by ' + 

sitemap view django

import datetime
from django.contrib.sitemaps import Sitemap

from home.models import Post

# Create your views here.

class Post_sitemap(Sitemap):
    changefreq = "daily"
    priority = 1.0
    #lastmod =
    def items(self):
        return Post.objects.all()

    def lastmod(self, Post):
        return Post.time_stamp

django sitemap url

#Call Your Function By

from django.conf.urls import url

from django.contrib.sitemaps.views import sitemap

from home.views import Post_sitemap

sitemaps = {
    'posts': Post_sitemap

#django sitemap_index.xml


urlpatterns = [

     path('sitemap_index.xml', sitemap, {'sitemaps': sitemaps},


django sitemap https

after installation visit your domain Like

On Local Server Go To Your localserver:



noimg Visit Link: