May 25, 2020, 7:22 p.m.

5 Steps to create a sitemap in Django

A sitemap is a file where we provide information about the web pages and other files of our site. Search engines like Google, Bing etc. read this file to more intelligently crawl our site.
It is a XML file that holds complete list of page URLs for a site along with other additional details (metadata of each URLs, when it was last updated etc.). It 's primary purpose is to inform search engines about pages on your sites that are available for crawling.
Sitemap is very helpful to increase your website' s rank on google.

Today i will show you that how can you create a xml sitemap of your django website easily. If you are using django for web development then you don't require any 3rd party tool to generate a sitemap for your website. It' s a super easy task to make a sitemap in django. I will keep the things as simple as possible and explain the whole procedure step by step.

Step 1 In settings.py file add
'django.contrib.sites' & 'django.contrib.sitemaps' in INSTALLED_APPS list and make a variable SITE_ID=1 under INSTALLED_APPS list as shown below :
#in settings.py

INSTALLED_APPS=[
.
.
'django.contrib.sites',
'django.contrib.sitemaps',
]
SITE_ID=1


Now, run the migrate command :
python manage.py migrate

Step 2 Create a sitemaps.py file in any app and write the code as shown below :

#in sitemaps.py

from django.contrib.sitemaps import Sitemap
from blog.models import Post

class PostSitemap(Sitemap):
    def items(self):
       return Post.objects.all()


Here i take an example of a blog app. I have just imported the Post model class from blog (you can use import * if you have more than 1 model).
Always remember that you have to make 1 class in sitemaps.py (of any name ,here PostSitemap) for each table (model) and define a "items" function in this class which will return all objects from the table.

Step 3 In urls.py file make a dictionary sitemaps and put all the class objects (from sitemaps.py) in it and assign a url pattern for your website's sitemap in urlpatterns list as shown below -
from django.contrib.sitemaps.views import sitemap
from app.sitemaps import PostSitemap
sitemaps={
'Posts':PostSitemap,

#put all sitemaps in this dictionary }

urlpatterns=[
.
.
.
path('sitemap.xml',sitemap,{'sitemaps':sitemaps}),
]


For this case the sitemap will be shown when we use the url
example.com/sitemap.xml

Step 4 In each and every model class which you want to include in sitemap, you have to put an get_absolute_url function .

from django.urls import reverse
class Post(models.Model):
    .
    .
    .
    def get_absolute_url(self):
       return ('description',args=[str(self.id)])

The above code will work for an url something like this :
example.com/description/1
If the url uses parameters/arguments then you have to pass these arguments in "args" list in "get_absolute_url" function.

Step 5 Now open the django admin app and move to Sites table to add and save a record for your website as shown below :
image not found
Tips Like the sitemap, robots.txt file is also an important file which is used by search engines while scanning or crawling a website.
If you want to learn how to create robots.txt file in django, you should read this article on how to generate robots.txt file in django






Some recent posts



Complete process of changing database from SQLite to MySQL - How to migrat...

" How to download a file in django | Making a large file downloadable from ...

How to use proxy in python requests | http & https proxies - Scraping...

Top Django Querysets, How to filter records | Fetching data from database e...

How to change base url or domain in sitemap - Django ...

How to Make a Website - Everything you should know about web development...

What is Javascript - Features and usages ...

Top 5 Interview Questions : Tips for HR round ...

How to get job in IT : Perfect resume guide for IT job ...

Programming vs coding | difference between programming and coding ...



View all...