Skip to main content

SitemapCfg

Django's sitemap framework is classic Django. It covers all Django functionality. It's class-based. It's configurable and flexible. It can reverse URLs and ping Google. Most of the time it's exactly what you want, and everything you could want. But not all of the time. Perhaps, like me, you use Django in an unusual way. The sitemap framework is not only difficult to use, it will not do some things I want. So I've made this configuration-based alternative. It's crude, but simple.

Why you would not want to use this app

  • Can only output URLs from model URL data, not views
  • Has no exclude/filter configuration, outputs everything
  • No checks for sitemap file-size, subfolder renaming etc.
  • Creates a sitemap index always, despite one sitemap only
  • Ignores most of the sitemap spec
  • Can only handle one site

Why you would want to use this app

  • Search engines also ignore most of the spec
  • 5 minuite setup in 'settings' file only

Documentation

Install

PyPi,

    pip install sitemap_cfg

Or download the app code to Django.

The module needs no database tables, but need to declare so the registered module can find models etc. Declare in Django settings,

    INSTALLED_APPS = [
        ...
        'sitemap_cfg.apps.SitemapCfgConfig',
    ] 

Configuration

Need to declare where the sitemap files will go,

    SITEMAP_DIR = PROJECT_DIR / "static"

The domain (usually set for cannonical),

    SITEMAP_DOMAIN = "https://freefalling.com"

Sitemap index name defaults to 'sitemap_index', but can be renamed,

    SITEMAP_INDEX_NAME = 'sitemap_main'

Then declare which models to use, or URLs to add. Keys are the sitemap filename,

    SITEMAP = {
        # Model names are just the app/model name (not the full path)
        # If model is the only data given, tries to use get_absolute_url()
        'sitemap_reviews' : [
            {'model': 'reviews.Review'},
        ],

        # Can use data from the model to construct URLs, usually a slug
        # 'url_path' is the intermediate path, so allows custom URL path 
        'sitemap_infopages' : [
            {'model': 'infopages.Infopage', 'field' : 'slug', 'url_path': 'info'},
        ],

        # can also declare one-off URLs. 
        # if preceeded by ''http' these are written as presented...
        # if written as stubs get prepended by SITEMAP_DOMAIN
        'sitemap_other' : [
            {'urls': ['credits', '', 'https://freefalling.com/' ]},
        ], 


        # Can have different collections of data in one sitemap
        'sitemap_specialist_reviews' : [
            {'model': 'reviews.SongReview'},
            {'model': 'datelists.ProductReviews'},
        ],
    }

The lastmod attribute

Most sitemap spec attributes are ignored. Google, and presumably other engines, say they ignore them ('priority' etc.). However, Google recognises, and encourages use of, the 'lastmod' attribute. Add 'lastmod' attributes to models like this,

    SITEMAP = {
        # Can set 'lastmod_field' on model datasets
        # Value is the fieldname to get the modification data from
        # The field is expected to be a Django DateField
        'sitemap_infopages' : [
            {'model': 'infopages.Infopage', 'field' : 'slug', , 'url_path': 'info', 'lastmod_field': 'modified'},
        ],
    }

This assumes the declared model field is a Django 'models.DateField'.

There's another option available. This is because it is possible to change a delivered webpage without changing the model. Instead, templates are changed. As a dramatic example, perhaps templates have been adapted with 'structured data' added, to deliver what Google calls 'search snippets'. Of course,, as time goes by, you would like Google (at least, but maybe Bing, Yandex etc.) to recrawl these pages to deliver the new data, and so the enhanced search listing. So a literal date can be declared, in the form YYYY-MM-DD,

    SITEMAP = {
        # Can set a literal value on 'lastmod_field'
        'sitemap_infopages' : [
            {'model': 'infopages.Infopage', 'field' : 'slug', 'lastmod_field': '2021-12-02'},
        ],

        # can also declare a literal value of 'lastmod_field' one-off URLs. 
        'sitemap_other' : [
            {'urls': ['credits', '', 'https://freefalling.com/'], 'lastmod_field': '2021-12-02'},
        ], 
    }

A thought: I considered a 'today' option, but that would update on every sitemap generation, so is wildly imprecise. And unhelpful to search engines. Also, mass updating of model 'modified' dates is not good for site maintenence, the page is updating, not the model data. A literal date will not be automatic, will need the Django Setting file changing and possibly reverting. Whatever, it leaves model data unaltered, as it should be. Once a search engine has crawled, the setting can be reverted to a model 'modified' field, and pages with no model will use the coded literal date, which is a fair representation.

Generate sitemaps

    ./manage.py mksitemap

End of...

Said it was simple.

Metadata

Release files for sitemap_cfg 0.2.0

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

Source distribution (sdist)

Source distribution for sitemap_cfg 0.2.0
File Size Uploaded Provenance
sitemap_cfg-0.2.0.tar.gz 6.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for sitemap_cfg 0.2.0
File Interpreter ABI Platform
sitemap_cfg-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.1 kB

Release files / sitemap_cfg-0.2.0.tar.gz

Download URL sitemap_cfg-0.2.0.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
88f9145846a60081290d4e2ca46675a2ec60540ce63e2d201ec0b58e50dda385
BLAKE2b-256 checksum
How to use checksums
f47957edd5a40be673b132bd5a520c506ffcaeea0b9de1bb063eb0403bf9bc8f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.22.0

Release files / sitemap_cfg-0.2.0-py3-none-any.whl

Download URL sitemap_cfg-0.2.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
514f51585fdcc974dff681a397e02d9e61f11086796a9bdaadd9af3613a9e92f
BLAKE2b-256 checksum
How to use checksums
e64b34d14dafab37d6391b30ac6e114e3cbd73659ed95c269faf2115c0502555
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.22.0

Security for sitemap_cfg 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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