In this article, we'll learn how to make sitemaps in Django, so if you want to have a sitemap for your website or blog, you are in the right place.

1. Django Project structure


├── core
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── __init__.py
│   ├── migrations
│   ├── models.py
│   ├── __pycache__
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── db.sqlite3
├── manage.py
├── media
├── pypro
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── static
└── TEMPLATES


2. Setting up our Django Sitemaps

settings.py

In settings.py, add the following line to INSTALLED_APPS.


# pypro/settings.py

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

sitemaps.py

Now, create a sitemaps.py file and put the following code in it, so in my case, I'll create the file into the pypro folder.


# pypro/sitemaps.py

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


class StaticViewSitemap(Sitemap):
    def items(self):
        return [''] # paths name
    def location(self, item):
        return reverse(item)

urls.py

Now, add a path for Django sitemap.


# pypro/urls.py

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

sitemaps = {
    'static': StaticViewSitemap
}

urlpatterns = [
	#...
	#sitemap
    path('sitemap.xml', sitemap, {'sitemaps': sitemaps}),
]


Done!

3. Adding FBV URL to sitemaps

First, we need to create a simple page view.

views.py


# core/views.py

def index(self):
    return HttpResponse("<h1>Index Page</h1>")

urls.py


# core/urls.py

from .views import *

urlpatterns = [

path('index/', index, name="index_page"),

]


Setting Up Django Sitemaps

Now, let's add our Views URL to sitemaps.

sitemaps.py


# pypro/sitemaps.py
from django.contrib.sitemaps import Sitemap
from django.shortcuts import reverse


class StaticViewSitemap(Sitemap):
    def items(self):
        return ['index_page'] # paths name
    def location(self, item):
        return reverse(item)

index_page: name that we've set for index view path

let's check out the sitemaps page


Setting Up Django Sitemaps

Great! it's working

4. Adding a dynamic URL to sitemaps

Let's say you have a blog website, and you want to add articles URL to sitemaps.

First, we'll prepare our articles DetailView.

models.py


# core/models.py

class Articles(models.Model):
    title = models.CharField(max_length=300, null=True, blank=True)
    content = models.TextField(null=True, blank=True)
    time = models.DateTimeField(auto_now_add=True)
    def __str__(self):
       return self.title


    def get_absolute_url(self):
        return reverse('Articles', args=(self.pk,))  

after adding some data.

Setting Up Django Sitemaps

Views.py


# core/models.py

class ArticleDetail(DetailView):
    model = Articles
    slug_field = 'pk'

urls.py


# core/urls	

#Articles
path('article/<slug:slug>/', ArticleDetail.as_view(), name='Articles'),

name='Articles' that we have used in reverse() function

now, we'll create The articles URL in ou sitemap.

sitemaps.py


# pypro/sitemaps.py
from django.contrib.sitemaps import Sitemap
from django.shortcuts import reverse

#models
from core.models import Articles


class StaticViewSitemap(Sitemap):
    def items(self):
        return ['index_page'] # paths name
    def location(self, item):
        return reverse(item)

class SnippetViewSitemap(Sitemap):
    def items(self):
        return Articles.objects.all()


urls.py


#pypro/urls.py

sitemaps = {
    'static': StaticViewSitemap,
    'snippet':SnippetViewSitemap,
}

result:

Setting Up Django Sitemaps

as you can see, the urls has been created

5. Sitemaps Options

in this part, we'll add some options to our Blog sitemaps, like changefreq priority lastmod

sitemaps.py


# pypro/sitemaps.py
class SnippetViewSitemap(Sitemap):
    changefreq = "never"
    priority = 0.5

    def items(self):
        return Articles.objects.all()

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



Setting Up Django Sitemaps

Great!

for more information, check out https://docs.djangoproject.com/en/3.1/ref/contrib/sitemaps/

6. Summary

In this article, I showed you my method to create sitemaps for websites.

if you want to get a responsive theme for your Django admin interface, check out this article: https://pytutorial.com/install-django-jet

English today is not an art to be mastered it's just a tool to use to get a result