How to add sitemap to your website using Django and Python

What is a sitemap? 

Django comes with a sitemap framework, which allows you to generate sitemaps for your site dynamically. A sitemap is an XML file that tells search engines the pages of your website, their relevance, and how frequently they are updated. Using a sitemap will make your site more visible in search engine rankings: sitemaps help crawlers to index your website’s content.

The Django sitemap framework automates the creation of this XML file by letting you express this information in Python code.

The Django sitemap framework depends on django.contrib.sites, which allows you to associate objects to particular websites that are running with your project. This comes in handy when you want to run multiple sites using a single Django project. To install the sitemap framework, you will need to activate both the sites
and the sitemap applications in your project.

Edit the settings.py file of your project and add django.contrib.sites and django.contrib.sitemaps to the INSTALLED_APPS setting. Also, define a new setting for the site ID, as follows

SITE_ID = 1
# Application definition

INSTALLED_APPS = [
    #...
    'django.contrib.sites',
    'django.contrib.sitemaps',
    
]

Now run the following command to create the tables of the Django site application in the database:

python3 manage.py migrate

Next, create a new file inside your blog application directory and name it sitemaps.py . Open the file and add the following code to it:

Let’s assume you have a blog system, with an Article model, and you want your sitemap to include all the links to your individual blog entries. Here’s how your sitemap class might look:

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

class ArticleSitemap(Sitemap):
    changefreq = 'weekly'
    priority = 0.9

def items(self):
    return Article.published.all()

def lastmod(self, obj):
    return obj.updated

You create a custom sitemap by inheriting the Sitemap class of the sitemaps module. The changefreq and priority attributes indicate the change frequency of your post pages and their relevance in your website (the maximum value is 1 ). The items() method returns the QuerySet of objects to include in this sitemap. By default, Django calls the get_absolute_url() method on each object to retrieve its URL.

The lastmod method receives each object returned by items() and returns the last time the object was modified.

Finally, you just need to add your sitemap URL. Edit the main urls.py file of your project and add the sitemap, as follows:

"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.contrib.sitemaps.views import sitemap
from news.sitemaps import ArticleSitemap
from django.conf.urls.static import static

sitemaps = {
'articles': ArticleSitemap,
}

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('news.urls', namespace='news')),
    path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap')
]

Now run the development server and open http://127.0.0.1:8000/sitemap.xml in my case the url is  https://www.thesocialtalks.com/sitemap.xml in your browser. You will see the following XML output:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<urlset xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9>

<url>
<loc>
https://thesocialtalks.com/9/india-coronavirus-and-disaster-delhis-nizamuddin/
</loc>
<lastmod>2020-04-03</lastmod>
<changefreq>weekly</changefreq>
<priority>0.9</priority>
</url>
<url>
<loc>
https://thesocialtalks.com/8/china-and-usa-relations-broken/
</loc>
<lastmod>2020-04-03</lastmod>
<changefreq>weekly</changefreq>
<priority>0.9</priority>
</url>
<url>
<loc>
https://thesocialtalks.com/7/you-and-covid19-there-way-out-pandemonium/
</loc>
<lastmod>2020-03-27</lastmod>
<changefreq>weekly</changefreq>
<priority>0.9</priority>
</url>
<url>
<loc>
https://thesocialtalks.com/6/pangs-child-mortality-society/
</loc>
<lastmod>2020-03-26</lastmod>
<changefreq>weekly</changefreq>
<priority>0.9</priority>
</url>
<url>
<loc>
https://thesocialtalks.com/5/living-along-river-lives-and-conditions-settlements-banks-river-yamuna-delhi/
</loc>
<lastmod>2020-03-25</lastmod>
<changefreq>weekly</changefreq>
<priority>0.9</priority>
</url>
<url>
<loc>
https://thesocialtalks.com/4/look-us-iran-tensions/
</loc>
<lastmod>2020-01-23</lastmod>
<changefreq>weekly</changefreq>
<priority>0.9</priority>
</url>
<url>
<loc>
https://thesocialtalks.com/3/more-human-less-humanity/
</loc>
<lastmod>2019-12-31</lastmod>
<changefreq>weekly</changefreq>
<priority>0.9</priority>
</url>
<url>
<loc>https://thesocialtalks.com/2/Illegal-immigration/</loc>
<lastmod>2020-01-26</lastmod>
<changefreq>weekly</changefreq>
<priority>0.9</priority>
</url>
<url>
<loc>
https://thesocialtalks.com/1/slippery-slope-misguided-feminism/
</loc>
<lastmod>2019-12-25</lastmod>
<changefreq>weekly</changefreq>
<priority>0.9</priority>
</url>

</urlset>

What are sitemap classes? 

Sitemap class is a Python class that represents a “section” of entries in your sitemap. For example, one Sitemap class could represent all the entries of your Weblog, while another could represent all of the events in your events calendar.

In the simplest case, all these sections get lumped together into one sitemap.xml, but it’s also possible to use the framework to generate a sitemap index that references individual sitemap files, one per section.

 

If you want to explore more about sitemap in Djnago, go through the official document on  Django website. Here is the link

https://docs.djangoproject.com/en/3.0/ref/contrib/sitemaps/#sitemap-classes

 

Leave a Reply