Skip to main content

sitemap.xml generation using lxml with support for alternates. It uses Python 3’s keyword-only arguments for self-documenting code.

Installation

Simply pip install django-sitemaps. The package consists of a single python module, django_sitemaps, containing the single class; there’s no additional configuration necessary.

Usage

View:

from app.pages.sitemaps import PagesSitemap

def sitemap(request):
    sitemap = Sitemap(
        # All URLs are passed through build_absolute_uri.
        build_absolute_uri=request.build_absolute_uri,
    )

    # URLs can be added one-by-one. The only required argument
    # is the URL. All other arguments are keyword-only arguments.
    for p in Page.objects.active():
        url = p.get_absolute_url()
        sitemap.add(
            url,
            changefreq='weekly',
            priority=0.5,
            lastmod=p.modification_date,
            alternates={
                code: urljoin(domain, url)
                for code, domain in PAGE_DOMAINS[p.language].items()
            },
        )

    # Adding conventional Django sitemaps is supported. The
    # request argument is necessary because Django's sitemaps
    # depend on django.contrib.sites, resp. RequestSite.
    sitemap.add_django_sitemap(PagesSitemap, request=request)

    # You can also specify the site and protocol manually should you wish
    # to do so:
    sitemap.add_django_sitemap(
        PagesSitemap, site=...site..., protocol=request.scheme
    )
    # Note! If you're omitting the request you *have* to specify site and
    # protocol yourself.

    # You could get the serialized XML...
    # ... = sitemap.serialize([pretty_print=False])
    # ... or use the ``response`` helper to return a
    # ready-made ``HttpResponse``:
    return sitemap.response(
        # pretty_print is False by default
        pretty_print=settings.DEBUG,
    )

URLconf:

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)),
    ...
]

The robots_txt function returns a view which can be used to generate a robots.txt file containing sitemap URLs. The default sitemap only contains:

User-agent: *
Sitemap: <protocol>://<host>/sitemap.xml

The list of sitemap URLs may be overridden by setting sitemaps:

from django.urls import reverse_lazy

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

Metadata

Release files for django-sitemaps 2.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-sitemaps 2.0.2
File Size Uploaded
django_sitemaps-2.0.2.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-sitemaps 2.0.2
File Interpreter ABI Platform
django_sitemaps-2.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 9.5 kB

Release files / django_sitemaps-2.0.2.tar.gz

Download URL django_sitemaps-2.0.2.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f69e750c41506ba063ec847897692c03b0e3588cdab927c987000fe54fbb79cd
BLAKE2b-256 checksum
How to use checksums
d1b3a4dd172d68a63c95a7a14cb8d4c8f2e645105aae7b7c73a25b0a513ba71e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release files / django_sitemaps-2.0.2-py3-none-any.whl

Download URL django_sitemaps-2.0.2-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
24905e4cc3717770878437c8bb0805ddb88d65d1341c7d1a4e77d710d9e54590
BLAKE2b-256 checksum
How to use checksums
78e20ad5aa69fd744704c6656ca7226f2a5300fec5f3e21a5ee0462c2c8aad61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release history Release notifications | RSS feed

This release

2.0.2 This release

2 release files

2.0.1

1 release file

2.0.0

2 release files

1.1.1

2 release files

1.1

2 release files

1.0.1

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page