Adding Sitemap to Django

Django comes with a sitemap package built in. The sitemap provides you with a .XML file that lists down your pages in xml format which is used by the search engine to crawl data

To use this we need to add site and sitemap package it to our INSTALLED_APPS in the settings.py file. It comes with Django so we just need to add these two lines in the INSTALLED_APPS

'django.contrib.sites',
'django.contrib.sitemaps',

we need to add a system variable called SITE_ID. and we will provide an id for the django site, lets set the id as 1.

SITE_ID = 1

Now we have to create the sitemaps.py file. Let's create the sitemaps.py file in our main app directory. This file will gather all the items of your website and list it. We need to import Sitemaps form the django.contrib.sitemaps package to use it.

from django.contrib.sitemaps import Sitemap
from .models import Posts

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

We create a PostSitemap class that is a subclass of our Sitemap class. It has a function called items that gets all the Posts of our blog and returns the value. To return the posts we have to import the Posts class from our model.

We need to make sure the sitemap is accessible to the search engine as the search engine will look for the file 'sitemaps.xml', lets create a url for the file. We DON"T have to create a sitemap.xml file as the sitemap package will handle that. what we need is to provide information that will go into the sitemap.xml file

in our main apps urls.py file we need to add a few things

from .sitemaps import PostSitemap
from django.contrib.sitemaps.views import sitemap

sitemaps ={
    'posts': PostSitemap,
}

urlpatterns = [
    ....
    ....

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

we imported the PostSitemap class from the sitemap.py file. and we imported the View for the xml 'sitemap' from the django.contrib.sitemaps.views class. This is the built-in view for xml. Then we created a sitemap dictionary. it will hold all the sitemaps we will create for the apps or static pages later. Finally we add the path to the url. we set the path to the sitemap.xml so that it is accessible to the search engines.

to check you can run the server and check it for yourself.

To add static pages to our sitemap we have to add another class in our sitemaps.py,

....
....
from django.urls import reverse

....
....

class StaticSitemap(Sitemap):
    def items(self):
        return ['blog-about']

    def location(self, item):
        return reverse(item)

The items function will serve the static page by its name, in our case it is the about page, which we named 'blog-about'. we need to provide a location function so that it can be mapped to the page. NOTICE that , items function returns a list, it means we can add all our static pages as the items of the list and all will be added to the sitemap !!

To get that information to our sitemap.xml we need to import the StaticSitemap class to the urls.py and update our sitemaps dictionary. we are done!

from .sitemaps import PostSitemap,StaticSitemap

sitemaps ={
    'posts': PostSitemap,
    'static': StaticSitemap,
}

Quick Access

Faster way to traverse the blog