Skip to main content
  1. My Blog Posts and Stories/

New Sitemap (Generating a Sitemap in Django)

··955 words·5 mins

New feature added #

Site map

What happened #

While I was checking up on the website, I noticed that there were a bunch of people who are filling up the contact form to offer paid services for SEO optimization.

What is SEO and Why should I care? #

SEO stands for Search Engine Optimization. It helps search engines like google display your webpage to other users who are searching for it online.

In a way you can think of it as making a website more attractive to search engines.

By having a good SEO, it makes it more likely for it to appear as a higher result on a particular search. It is a good way to generate organic views for a website (Such as this one).

To learn more about SEOs, you can visit the link here

This are the things I can do to improve my SEO

  1. Add a site map
  2. Add metadata to the website
    1. Keywords / Phrases
    2. Summary of the page
  3. Add alt tags to images
  4. Publish more relevant content
  5. Update content regularly
  6. Attract links from other websites

There are some of them which I can work on in my website and some others which depend on other people who find my blog interesting. With that in mind I decided to do each of them slowly one step at a time as there are many changes which has to be made for each of them to be implemented.

Maybe I should have thought of this earlier to avoid the hassle I have to deal with now.

What is a sitemap? #

A sitemap is a file where we provide information about the pages and files on our website and the relationship between them.

This allows search engines like google to crawl a site more effectively. It tells them which files are the main ones to focus on.

For more information check out this

Time to dive into the details (Implementing it in Django) #

Lucky for me, there is a built in middleware in Django that allows me to generate a site map. It is called the sitemaps framework.

Step 1: Installation #

To install the application into your django app, add the following to INSTALLED_APPS under your file as shown below.

    'django.contrib.sitemaps', # Add this

SITE_ID = 1 # Add this as well

What does the SITE_ID do? It marks the domain id for the website in case the Django application is serving multiple websites.

Step 2: Adding the path #

To start, add the following to your file. This will make the /sitemap.xml path point to the generate site map file

# config/
from django.contrib import admin
from django.urls import path, include
from django.contrib.sitemaps.views import sitemap

## We will modify this later to add the site maps in. We can leave this blank for now
sitemaps = {


urls = [
    path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap') # Add the path to the sitemap

Adding the Sitemap #

To add a sitemap, we will need to create a new class which inherits from the Sitemap superclass and put it into the

There are different types of pages to create the sitemap. Generally they are split into two categories - static and dynamic.

For each of there there is a priority and a change frequency.

  • The priority tells is the site map which ones are the most important pages on the website.
  • The Change frequency tells the search engine how often the page is updated.

Static Pages #

For static pages, we can define a fixed Sitemap class. In this case, I am creating a site map for my homepage.

from django.contrib.sitemaps import Sitemap
from django.urls import reverse

class HomepageSitemap(Sitemap):

    priority = 1.0
    changefreq = 'yearly'

    def items(self): # Tells them which function(s) is the sitemap for.
        return ['index']

    def location(self, item): # Looks for the url path of the function.
        return reverse(item)

This will show up as

<urlset xmlns="" xmlns:xhtml="">

Dynamic Pages #

For dynamic pages, we can define a dynamic Sitemap class. In this case, I am creating the site map for the blog.

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

class BlogSitemap(Sitemap):
    changefreq = "never"
    priority = 0.5

    def items(self):
        return Entry.objects.filter(is_draft=False) # Getting all blog posts which are not drafts

    def lastmod(self, obj):
        return obj.last_modified_date # This is the last modified date of the blog post

In this case, the items are the blog entries in the model and the lastmod is the last modified date for the blog.

This will eventually show up as the following assuming that I have 2 blog post with id 10 and 11

<urlset xmlns="" xmlns:xhtml="">

Adding the pages in #

Now that the sitemap is created for the homepage and the blog, we can proceed to add it in the file from earlier.

# config/
from blog.sitemaps import BlogSitemap
from homepage.sitemaps import HomepageSitemap

## We will modify this later to add the site maps in. We can leave this blank for now
sitemaps = {
    'blog': BlogSitemap(),          # Add the blog sitemap in
    'homepage': HomepageSitemap(),  # Add the homepage sitemap in

After adding it in you should be able to find the sitemap in the /sitemap.xml route. In the case for my website its here

Conclusion #

I will be doing more to improve the SEO of the website. Hopefully this will help me become more famous and well know hur hur hur. Next time I can do something more about the SEO. See you guys on the first page of google :D. Till next time :D.