Sitemap Framework Configuration
To initialize the sitemap framework in your Django project, you must register the sitemaps application and configure your URL patterns to serve the sitemap XML files.
Basic Configuration
To enable the sitemap framework, add django.contrib.sitemaps and django.contrib.sites to your INSTALLED_APPS and define a SITE_ID. The SiteMapsConfig class in django/contrib/sitemaps/apps.py handles the application initialization, including setting the default auto field to django.db.models.AutoField.
# settings.py
INSTALLED_APPS = [
"django.contrib.sites",
"django.contrib.sitemaps",
# ... your other apps
]
SITE_ID = 1
Defining a Sitemap Class
Create a subclass of django.contrib.sitemaps.Sitemap to define which URLs should be included. You must implement the items() method, which returns a list of objects.
# sitemaps.py
from django.contrib.sitemaps import Sitemap
from .models import Entry
class BlogSitemap(Sitemap):
changefreq = "never"
priority = 0.5
def items(self):
# Returns the objects to include in the sitemap
return Entry.objects.filter(is_draft=False)
def lastmod(self, obj):
# Returns the last modification date for an object
return obj.pub_date
Configuring URL Patterns
You can serve a single sitemap or a sitemap index that points to multiple sectioned sitemaps. Use the views provided in django.contrib.sitemaps.views.
# urls.py
from django.contrib.sitemaps import views
from django.urls import path
from .sitemaps import BlogSitemap
sitemaps = {
"blog": BlogSitemap,
}
urlpatterns = [
# Sitemap index (useful for large sites)
path(
"sitemap.xml",
views.index,
{"sitemaps": sitemaps},
name="django.contrib.sitemaps.views.index",
),
# Individual sitemap sections
path(
"sitemap-<section>.xml",
views.sitemap,
{"sitemaps": sitemaps},
name="django.contrib.sitemaps.views.sitemap",
),
]
Using GenericSitemap for Quick Setup
If you don't need custom logic, use GenericSitemap to generate a sitemap directly from a queryset. This is frequently used for simple models as seen in tests/sitemaps_tests/urls/http.py.
from django.contrib.sitemaps import GenericSitemap
from .models import TestModel
generic_sitemaps = {
"generic": GenericSitemap(
{
"queryset": TestModel.objects.order_by("pk").all(),
"date_field": "lastmod",
},
priority=0.6
),
}
# In urlpatterns:
# path("sitemap-generic.xml", views.sitemap, {"sitemaps": generic_sitemaps})
Internationalization (i18n) Support
To generate sitemaps for multilingual sites, set the i18n attribute to True on your Sitemap class. You can also enable alternates to include xhtml:link tags for other language versions.
class SimpleI18nSitemap(Sitemap):
i18n = True
alternates = True
x_default = True
def items(self):
return I18nTestModel.objects.all()
Troubleshooting and Gotchas
- Template Loading: Even though the sitemap framework doesn't use database tables, it must be in
INSTALLED_APPS. This ensures Django's template loader can find the default XML templates required to render the sitemap views. - Sites Framework Dependency: If
django.contrib.sitesis not installed, the sitemap views will raise anImproperlyConfigurederror unless you manually pass aSiteorRequestSiteobject to the view. - Last-Modified Header: The
Last-ModifiedHTTP header is only sent by the sitemap view if all items returned by theSitemap.items()method have a validlastmodvalue. If even one item returnsNoneforlastmod, the header will be omitted. - Sitemap Location: Search engines typically only index links that are at the same directory level as the sitemap or deeper. It is highly recommended to place your sitemap at the root URL (e.g.,
/sitemap.xml).