Instagram logo to direct to CountMe tech ig profile page   LinkedIn logo that is directed to CountMe tech profile page

First of all, let’s find out what a sitemap is and why it is important to have one for your website. A sitemap is a document that contains all the important URLs within your website. Google bots use it as a navigation tool for your websites. The bots crawl and index your webpages for certain key terms much faster and efficiently.

So why are sitemaps needed for your website? Sitemaps automate the submission of your webpages to Google. Therefore, instead of manually submitting your new web pages every time for indexing. The new webpage URL link is automatically added to the sitemap documentation and hence crawled and indexed much quicker. Consequently, this is one of the many methods  a website such as CountMe implements to improve its Google Search Engine Optimisation (SEO).

Let’s create a sitemap documentation for a Django website. In this blog, I’ll show you a step-by-step process on how to include static pages and dynamic pages to the sitemap document. 

 

Step 1: Install the Django sites app

Go to myenv > myproject > inventory > settings.py

Where myproject = your project name folder and 

inventory = folder location of settings.py file

# Application definition
 
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main.apps.MainConfig', 
    'django.contrib.sites', # add sites to installed_apps
    
]
SITE_ID = 1 # define the site id

Add the Django sites (django.contrib.sites) to the installed apps and define the SITE_ID as seen above.

Run migrations in the terminal to add the sites table to the database.

python manage.py makemigrations

python manage.py migrate

 

Step 2: Add your domain name to the Sites

  • On your terminal run - python manage.py runserver
  • Locate and log in to the Django Admin using your credentials
  • Head to the SITES section and click on Sites

Sites section in Django Admin for sitemap

Figure 1: Sites section in Django Admin

  • Change the domain and display names to the appropriate name. In production; change it to your chosen domain name and while in development; change it to 127.0.0.1:8000 (see image below).

Selected Site to change in Django Admin for sitemap

Figure 2: Selected Site to change in Django Admin

 

Step 3: Install the Django sitemap application

Go to myenv > myproject > inventory > settings.py

# Application definition
 
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main.apps.MainConfig', 
    'django.contrib.sites', # add sites to installed_apps
    'django.contrib.sitemaps',   #add Django sitemaps to installed apps
    
]

Add the Django sitemaps (django.contrib.sitemaps) to the installed apps as seen above.

 

Step 4: Create a new Django sitemap file 

Go to myenv > myproject > inventory > sitemaps.py (New file).

Add the following codes to your sitemaps.py. In this sitemaps.py file, a dynamic blog sitemap and a static sitemap class will be created. First import the necessary libraries; The imported Post model contains all the blogs that you would like on your sitemap.

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

Dynamic Django Sitemap - Create a BlogSitemap Class

This class returns page URLs using “location”, frequency at which page changes using “changefreq” and priority of pages using “priority”. The priority for pages goes from scale 0 to 1. The protocol is either “http” or “https”, use “https” in production environments.

class BlogSitemap(Sitemap):
    changefreq = "monthly"
    priority = 0.8
    protocol = 'http'
 
    def items(self):
        return Post.objects.all()
    
    def lastmod(self, obj):
        return obj.publish
    
    def location(self, obj):
        return '/blog/%s' % (obj.slug)

The items() method returns all the model objects we want listed on the sitemap. From the BlogSitemap class above, the items() method returns all blog posts from the Post model.

The location() method returns the URL location of the blog page. The Django sitemap framework usually calls the absolute URL, such as 127.0.0.1:8000/blog-name. Therefore, if your blog URLs look anything such as 127.0.0.1:8000/blog/blog-name, it is then important to return the blog slug first before the dynamic blog-name slug. This can be seen above in the BlogSitemap class location function.

Static Django Sitemap - Create a StaticSitemap Class

class StaticSitemap(Sitemap):
    changefreq = "yearly"
    priority = 0.8
    protocol = 'http'
 
    def items(self):
        return ['index', 'contactus'] #returning static pages; home and contact us
 
    def location(self, item):
        return reverse(item) #returning the static pages URL; home and contact us

The items() method returns the names of static pages within your project, these URL names are called and reversed in the location() method to get the absolute URL of those static pages.

You can add static pages such as home page and contact us pages, the URL for my home page is connected to the name “index” and the URL for my contact us page is connected to the name “contactus”. 

You decide which static pages you want seen in your sitemaps, find and select it’s URL name from its application, then include it in the items() method on the StaticSitemap class.

myenv > myproject > staticurlsapplication > urls.py

urlpatterns = [
    path('home', views.index, name='index'), #URL name for home page
    path("contactUs", views.contact_us, name="contactus"),#URL name for contactus page
    
]

 

Step 5: Add the Django sitemap URL

Go to myenv > myproject > inventory > urls.py

Add the following codes.

from django.urls import path
from . import views
from django.contrib.sitemaps.views import sitemap
from .sitemaps import StaticSitemap, BlogSitemap #import StaticSitemap
 
 
sitemaps = {
    'static':StaticSitemap, #add StaticSitemap to the dictionary
    'blog':BlogSitemap #add DynamicSitemap to the dictionary
}
 
urlpatterns = [
    
    path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),

 

Step 6: View the Django sitemap

The Django sitemap should now be available at http://127.0.0.1:8000/sitemap.xml. In this file, you should see all your blog’s URL and any static page URL that you added.

Django Sitemap URLs

Figure 3: Django Sitemap URLs