Hi, I'm Matthias

I am a founding partner of Feinheit AG and Die Bruchpiloten AG. Find me on GitHub, Mastodon, Bluesky, LinkedIn or by email.

2018-04-11

Django, Sitemaps and alternates

Django’s own sitemaps module is great for quickly generating sitemaps, but it unfortunately does not support more than the bare minimum of attributes. Also, it uses the template engine to create XML and this makes me sad.

We had to add a sitemap.xml file to a customers’ website. The site supports several languages and runs on several domains, so we had to carefully specify alternate language pages and canonical URLs so that the website would not be punished for duplicate content.

Therefore, a year ago I set out to build a sitemaps app for Django which supports Django’s sitemaps, but also allows adding entries with additional attributes. The result of this work was django-sitemaps.

from django_sitemaps import Sitemap

def sitemap(request):
    sitemap = Sitemap(build_absolute_uri=request.build_absolute_uri)
    sitemap.add_django_sitemap(SomeSitemap, request=request)
    sitemap.add(
       url,
       changefreq='weekly',
       priority=0.5,
       lastmod=datetime.now(),
       alternates={
           'en': '...',
           'en-ch': '...',
           'en-gb': '...',
           'de': '...',
           ...
       },
   )
   return sitemap.response(pretty_print=True)

Today, I also added support for generating the most simple robots.txt files possible: All user agents, and only Sitemap: <absolute url> entries. The recommended usage is now (still using url() instead of path() because I’m old and rusty):

from django_sitemaps import robots_txt
from app.views import sitemap

urlpatterns = [
    url(r'^sitemap\.xml$', sitemap),
    url(r'^robots\.txt$', robots_txt(timeout=86400)),
    ...
]