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
| Attribute | Type | Description |
|---|
| limit | int = 50000 | Maximum number of URLs to include in a single sitemap page as defined by the sitemap protocol index documentation. |
| protocol | string = null | If protocol is None, the URLs in the sitemap will use the protocol with which the sitemap was requested. |
| i18n | boolean = false | Enables generating URLs for all languages. |
| languages | list = null | Override list of languages to use. |
| alternates | boolean = false | Enables generating alternate/hreflang links. |
| x_default | boolean = false | Add an alternate/hreflang link with value 'x-default'. |
Constructor
Signature
Methods
get_languages_for_item()
@classmethod
def get_languages_for_item(
item: object
) - > list
Languages for which this item is displayed.
Parameters
| Name | Type | Description |
|---|
| item | object | The specific content item to check for language availability. |
Returns
| Type | Description |
|---|
list | A 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
| Type | Description |
|---|
[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
| Type | Description |
|---|
list | An 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
| Name | Type | Description |
|---|
| item | object | The content object for which the URL is being generated. |
Returns
| Type | Description |
|---|
string | The 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
| Name | Type | Description |
|---|
| protocol | string = null | An optional protocol override. |
Returns
| Type | Description |
|---|
string | The 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
| Name | Type | Description |
|---|
| site | object = null | An optional Site or RequestSite object to provide the domain. |
Returns
| Type | Description |
|---|
string | The 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
| Name | Type | Description |
|---|
| page | integer = 1 | The page number of the sitemap to retrieve. |
| site | object = null | The site object used to determine the domain. |
| protocol | string = null | The protocol to use for the generated URLs. |
Returns
| Type | Description |
|---|
list | A 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
| Type | Description |
|---|
datetime | The latest modification date found, or None if no lastmod attribute is defined. |