Lesson 11: Creating Sitemaps for Our Blog Application in Django 1.11.4 and Python 3.5

Welcome to this lesson, great to have you on board. My name is Henry and I am excited to continue with this tutorial series where we are building blog application using Django 1.11.4 and Python 3.5. We are in our eleventh lesson, creating sitemaps for our blog application. In the previous lesson, we learned how to create RSS feeds for our blog posts.

Django comes with a sitemaps framework which allows you to generate sitemaps for your website dynamically. A sitemap is an xml file that tells search engines like Google and Bing the pages on your website, their relevance and how frequently these pages are updated. By using a sitemap, you will help web crawlers to index your website content. The Django sitemaps framework depend on django.contrib.sites which allows you to associate object to a particular website running within your Django project. This comes handy when you want to run multiple sites using a single Django project. To learn more about Django sitemaps visit sitemaps framework.

NB: Before you continue, you need to install tinymce in you environment: run this command pip install django-tinymce

To install this sitemaps framework in our blog application project, we need to activate both the sites and sitemaps application in our project. Edit settings.py file and make sure it has the following code:

In our installed apps list, On line 42, we have added django.contrib.sites, on line 43, we have added the django.contrib.sitemaps and on line 126, we have added SITE_ID = 1 where we define a setting for the site id. Now we need to create table for sites in our database, to do this we execute the following command: python manage.py migrate Here is a screen-shot of my terminal:

The site application is now synced with our database. Inside our blog application, we are going to create a new file called sitemaps.py file. Make sure sitemaps.py file has the following code:

On line 1, we import site map library and on line 2, we import our blog post model. On line 4, we create a class called PostSitemap and we pass the Sitemap inside this class PostSitemap(Sitemap) thus our class is a subclass of Django sitemaps class. The changefreq and priority attribute indicates the change of frequency of the post pages and the relevance on the website respectively.

On line 8, we define items method which retrieve all published post. By default Django calls the get_absolute_url method on each object to retrieve it’s url. On line 11, we define the lastmod method receive each object retrieved by the item methods and returns the last time the object was modified.

Now edit the main urls.py file located on my site and make sure it has the following code:

On line 3, we are importing Django sitemaps, on line 4, we are importing PostSitemap class from our blog sitemaps.py file. On line 6 – 8, we create a sitemap dictionary. On line 14, we create a url pattern that matches sitemaps.xml, we pass the sitemaps dictionary which uses sitemaps views. Spin up your development server by running the following command: python manage.py runserver and open this url: http://localhost:8000/sitemap.xml you should see the following output on your browser:

Awesome! We have our sitemap.xml working correctly. However, if you take a closer look, you see that our blog post have been created using domain example.com instead of our localhost url. By default Django creates example.com site and we can change this in our Django admin. To login to our admin dashboard, let’s open this url: http://localhost:8000/admin/ you will see the following output

Click on the site and add the localhost to the sites.

Goals achieved in this lesson

  • We learned how to activate sites and sitemaps in Django.

  • We learned how to create Sitemap class in sitemaps.py file.

  • We learned how to add site id in Django settings.

  • We have learned how we can change the site in Django admin.

  • We have successfully created sitemap.xml for our blog application

With that we conclude our eleventh lesson, to get the code visit building a blog application. See you in lesson 12.

Facebook Comments
Get free email updates!
Signup now and receive an email once we publish new content.
We respect your privacy

Henry Mbugua

I am a Junior Developer ready to enter major leagues. My professional ambition is to stay versatile and be able to fill different roles within a product group. As such, I have been learning technology across frontend and backend.