Last modified: Jan 10, 2023 By Alexander Williams
Django Sitemap | How to Setup a Sitemap for Your App
In this article, we'll learn how to Create sitemap for 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 Django Sitemaps
In settings.py, add the following line to INSTALLED_APPS.
# pypro/settings.py
INSTALLED_APPS = [
'django.contrib.sitemaps',
]
Create sitemaps.py into your project folder, in my case I'll create it into pypro folder
Now, put the following line:
# pypro/sitemaps.py
from django.contrib.sitemaps import Sitemap
from django.shortcuts import reverse
class StaticViewSitemap(Sitemap):
def items(self):
return [''] # path's 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. Creating sitemaps For a simle view
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"),
]
http://localhost:8000:
Now, let's add our Views URL to the sitemaps.
sitemaps.py:
# pypro/sitemaps.py
from django.contrib.sitemaps import Sitemap
from django.shortcuts import reverse
#Creating the sitemap
class StaticViewSitemap(Sitemap):
def items(self):
return ['index_page'] # path's 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.
4. Creating Django sitemaps with a dynamic URL
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.
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'),
Now, we'll add The articles' URL to our 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:
as you can see, the urls has been created.
5. Sitemaps Options
after Creating the sitemaps, now 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
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