Skip to main content

No project description provided

Project description

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

Project details


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.1.tar.gz (4.8 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: django_sitemaps-2.0.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.6.0 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.7

File hashes

Hashes for django_sitemaps-2.0.1.tar.gz
Algorithm Hash digest
SHA256 b55cfcd1ce963f09adac4313952fea46273a0aba0ddbf78957c4436b956721e0
MD5 a2956d7c2829ced53f9dc64e2c4c742c
BLAKE2b-256 cb9677d3ded9c3158fa37324450a13a48fd5d37d89f5234eb89e6f9945d34d39

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page