NoReverseMatch at /sitemap.xml

Context

Recently add sitemap index as I have >50,000 urls.

Following the documentation: The sitemap framework | Django documentation | Django

NoReverseMatch at /sitemap.xml

https://example.com/sitemap.xml return 500 error with this error message

Reverse for 'django.contrib.sitemaps.views.sitemap' not found. 'django.contrib.sitemaps.views.sitemap' is not a valid view function or pattern name.

But these sections work

Code

# sitemaps.py
from django.contrib.sitemaps import Sitemap
from django.shortcuts import reverse
from something.models import Something


class StaticViewSitemap(Sitemap):
    def items(self):
        return ["home:index", "home:privacy", "home:terms"]

    def location(self, item):
        return reverse(item)


class SomethingSitemap(Sitemap):
    limit = 5000

    def items(self):
        return Something.objects.filter(is_hidden=False).order_by("-pk")

    def lastmod(self, obj):
        return obj.updated_at
# urls.py
sitemaps = {
    "static": StaticViewSitemap,
    "something": SomethingSitemap,
}

urlpatterns = [
   ...
    path(
        "sitemap.xml",
        views.index,
        {"sitemaps": sitemaps},
        name="django.contrib.sitemaps.views.index",
    ),
    path(
        "sitemap-<section>.xml",
        views.sitemap,
        {"sitemaps": sitemaps},
        name="django.contrib.sitemaps.views.sitemap",
    ),
]

Attempts

I tried sitemap_url_name no effect.

I tried adding location to SomethingSitemap, no effect.

Found the root cause.

Solution is to move the sitemap to yourproject.urls.py from yourapp.urls.py

This is mentioned in the documentation: The sitemap framework | Django documentation | Django

Specifically:

The name of the sitemap file is not important, but the location is. Search engines will only index links in your sitemap for the current URL level and below. For instance, if sitemap.xml lives in your root directory, it may reference any URL in your site. However, if your sitemap lives at /content/sitemap.xml, it may only reference URLs that begin with /content/.