Skip to main content

Sitemap

This class provides a framework for generating sitemaps, allowing for the definition of URL locations, priorities, and update frequencies. It supports pagination for large datasets and includes built-in functionality for internationalization, such as generating alternate language links and handling x-default tags. The class integrates with site and translation frameworks to resolve domains and localized paths dynamically.

Attributes

AttributeTypeDescription
limitint = 50000Maximum number of URLs to include in a single sitemap page as defined by the sitemap protocol index documentation.
protocolstring = nullIf protocol is None, the URLs in the sitemap will use the protocol with which the sitemap was requested.
i18nboolean = falseEnables generating URLs for all languages.
languageslist = nullOverride list of languages to use.
alternatesboolean = falseEnables generating alternate/hreflang links.
x_defaultboolean = falseAdd an alternate/hreflang link with value 'x-default'.

Constructor

Signature

def Sitemap()

Methods


get_languages_for_item()

@classmethod
def get_languages_for_item(
item: object
) - > list

Languages for which this item is displayed.

Parameters

NameTypeDescription
itemobjectThe specific content item to check for language availability.

Returns

TypeDescription
listA list of language codes applicable to the provided item.

paginator()

@classmethod
def paginator() - > [Paginator](../../core/paginator/paginator.md?sid=django_core_paginator_paginator)

Provides a paginator instance to split sitemap items into chunks based on the defined limit.

Returns

TypeDescription
[Paginator](../../core/paginator/paginator.md?sid=django_core_paginator_paginator)A Django Paginator instance configured with the sitemap's items and limit.

items()

@classmethod
def items() - > list

Returns the list of objects to be included in the sitemap; intended to be overridden by subclasses.

Returns

TypeDescription
listAn empty list by default, to be populated with content objects in subclasses.

location()

@classmethod
def location(
item: object
) - > string

Returns the absolute URL for a given item by calling its get_absolute_url method.

Parameters

NameTypeDescription
itemobjectThe content object for which the URL is being generated.

Returns

TypeDescription
stringThe absolute URL path of the item.

get_protocol()

@classmethod
def get_protocol(
protocol: string = null
) - > string

Determines the protocol (http or https) to use for sitemap URLs based on instance settings or request context.

Parameters

NameTypeDescription
protocolstring = nullAn optional protocol override.

Returns

TypeDescription
stringThe protocol string to be used in URL generation.

get_domain()

@classmethod
def get_domain(
site: object = null
) - > string

Retrieves the domain name for the sitemap, typically using the Django sites framework if no site object is provided.

Parameters

NameTypeDescription
siteobject = nullAn optional Site or RequestSite object to provide the domain.

Returns

TypeDescription
stringThe domain name of the site.

get_urls()

@classmethod
def get_urls(
page: integer = 1,
site: object = null,
protocol: string = null
) - > list

Generates the metadata for all URLs on a specific page of the sitemap.

Parameters

NameTypeDescription
pageinteger = 1The page number of the sitemap to retrieve.
siteobject = nullThe site object used to determine the domain.
protocolstring = nullThe protocol to use for the generated URLs.

Returns

TypeDescription
listA list of dictionaries containing URL metadata including location, lastmod, and changefreq.

get_latest_lastmod()

@classmethod
def get_latest_lastmod() - > datetime

Identifies the most recent modification date across all items in the sitemap.

Returns

TypeDescription
datetimeThe latest modification date found, or None if no lastmod attribute is defined.