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
- https://example.com/sitemap-static.xml
- https://example.com/sitemap-something.xml
- https://example.com/sitemap-something.xml?p=2
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.