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 crawl our site.
It is an XML file that holds a complete list of page URLs for a site along with other additional details (metadata of each URL, when it was last updated etc.). Its 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 how can you create an 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 things as simple as possible and explain the whole procedure step by step.

Step 1

In the 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 the 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 the urls.py file make a dictionary sitemap and put all the class objects (from sitemaps.py) in it and assign a URL pattern for your website's sitemap in the URL patterns 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 the sitemap, you have to put a 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 the "args" list in the "get_absolute_url" function.

Step 5

Now open the Django admin app and move to the Sites table to add and save a record for your website.

Recent Articles

Changing Database from SQLite to MySQL - Django

One of the main benefit to use django for web development is that it comes with an inbuilt database/filesystem i.e SQLITE. Managing database and files is super easy in django. But there are some drawbacks of using SQLITE...

  - 2023-07-12
How to download a file in django

Sometimes we have to serve a file which may be a large file from our project directory. Suppose you want to transfer a file from your project directory to your friend or to some other person/server/platform....

  - 2023-07-12
How to change base url or domain in sitemap - Django

A sitemap is the first important SEO step which elaborates a website's content to search engines. It provides information to search engines about the available content on our site, which helps them to crawl webpages for indexing....

  - 2023-07-12
How to Make a Website

After reading this article you will never ever search for web development guide neither on Google nor on YouTube....

  - 2023-07-12
What is Javascript - Features and usages

I'm kind of a person who always likes to get started by installing the things & writing some code. I love that but this time JavaScript requires a little bit of the theoretical basic so that we can understand what things we are learning, how hard and powerful it is and where they can be applied....

  - 2023-07-12
Top 5 Interview Questions : Tips for HR round

In this article we are talking about the most famous HR questions and the mind-set to answer those HR questions....

  - 2023-07-12
How to get job in IT - Perfect resume guide

In today's article I want to talk about how to prepare the best resume to get a job in IT. Yes, everybody wants to have a job and in order to get a job you have to apply for it and when you apply for that, the resume is one good thing that you always submit....

  - 2023-07-12


^