Sitemaps are very critical for SEO. For a Django application, how can we add sitemap? The Django applications has dynamic content and it is almost impossible to recreate a sitemap and upload to the site. The Django sitemap framework is for our rescue.

Step 1 

First of all, we have to add the django sitemap framework to the installed app as 'django.contrib.sitemaps'

Step 2

Create a sitemaps.py file and add following code

"""Sitemap for ATEMON Blog."""

from django.contrib.sitemaps import GenericSitemap
from models import BlogPost

blog_dict = {
    'queryset': BlogPost.objects.order_by('-pk').all(),
    'date_field': 'publish_date',
}

sitemaps = {
    'blogpost': GenericSitemap(blog_dict, priority=0.7, changefreq='monthly'),
}

The blog_dict hasthe queryset that returns all blog Posts. We can add filters to include only published blogs.

Step 3

In url.py addthe following

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

...

urlpatterns += [
     url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
]

Now you can go to /sitemap.xml on local host to seethe sitemap