Django
Dajngo SEO & Ads
Django: How to Add Sitemap?
Posted: January 09, 2026
|
Updated: January 09, 2026
Django: How to Add Sitemap?
Posted: November 12, 2025 | Updated: November 12, 2025Django has built-in support for it via the django.contrib.sitemaps framework. Below is a complete step-by-step guide:
Step 1: Enable the Sitemap Framework
In your settings.py, make sure the following app is added:
INSTALLED_APPS = [
# other apps...
'django.contrib.sites',
'django.contrib.sitemaps',
]
Also, set your SITE_ID (if you’re using the sites framework):
SITE_ID = 1
If you’re not using django.contrib.sites, that’s fine — Django will still generate sitemaps correctly.
Step 2: Create a Sitemap Class
In one of your apps (for example, blog), create a file called sitemaps.py.
Example:
# blog/sitemaps.py
from django.contrib.sitemaps import Sitemap
from django.urls import reverse
from .models import Post # your model
class PostSitemap(Sitemap):
changefreq = "weekly"
priority = 0.8
def items(self):
return Post.objects.filter(published=True)
def lastmod(self, obj):
return obj.updated_at
You can also make a sitemap for static views:
class StaticViewSitemap(Sitemap):
priority = 0.5
changefreq = "monthly"
def items(self):
return ['home', 'about']
def location(self, item):
return reverse(item)
Step 3: Add a Sitemap URL
In your project’s main urls.py, include the sitemap view:
# project/urls.py
from django.contrib import sitemaps
from django.contrib.sitemaps.views import sitemap
from django.urls import path, include
from blog.sitemaps import PostSitemap, StaticViewSitemap
sitemaps = {
'posts': PostSitemap,
'static': StaticViewSitemap,
}
urlpatterns = [
path('admin/', admin.site.urls),
path('sitemap.xml', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
]
Step 4: Verify It Works
Run your development server:
python manage.py runserver
Then visit:
http://localhost:8000/sitemap.xml
You should see an XML sitemap with your URLs listed.
Also, do not forget to add your web app's sitemap to Google Webmaster Tools and Bing Webmaster Tools.