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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_sitemaps-2.0.2.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_sitemaps-2.0.2-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file django_sitemaps-2.0.2.tar.gz.

File metadata

  • Download URL: django_sitemaps-2.0.2.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for django_sitemaps-2.0.2.tar.gz
Algorithm Hash digest
SHA256 f69e750c41506ba063ec847897692c03b0e3588cdab927c987000fe54fbb79cd
MD5 17407ee9aba555bba9a7b5ee0fb662f3
BLAKE2b-256 d1b3a4dd172d68a63c95a7a14cb8d4c8f2e645105aae7b7c73a25b0a513ba71e

See more details on using hashes here.

File details

Details for the file django_sitemaps-2.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_sitemaps-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 24905e4cc3717770878437c8bb0805ddb88d65d1341c7d1a4e77d710d9e54590
MD5 3ec9ce07bbf289ff89a9c5fdff19e1bd
BLAKE2b-256 78e20ad5aa69fd744704c6656ca7226f2a5300fec5f3e21a5ee0462c2c8aad61

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2.0.2 This release

2 files

2.0.1

1 file

2.0.0

2 files

1.1.1

2 files

1.1

2 files

1.0.1

2 files

1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page