Python Django Framework Sitemap Installation

Python-Django-Framework

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 https://coder-i.com/sitemap.xml 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 settings.py
#Add Exact Path On Your Models In models.py
#Create Your function on views.py
#Call Your Function By urls.py

django sitemap framework

#Add On your Istalled Apps On settings.py
INSTALLED_APPS = [    
    'django.contrib.sitemaps',    
]

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 models.py
#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 ' + self.author 

sitemap view django

import datetime
from django.contrib.sitemaps import Sitemap

#DataBase
from home.models import Post

# Create your views here.

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

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

django sitemap url

#Call Your Function By urls.py


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},
     name='django.contrib.sitemaps.views.sitemap'),

]

django sitemap https

after installation visit your domain Like
        https://your_domain/sitemap_index.xml

On Local Server Go To Your localserver:

      http://localhost:8000/sitemap_index.xml

 

noimg Visit Link: digitalocean.com