{"record":{"id":"3dc1af8a2700202b","repo":"django/django","slug":"page-s-empty","errorCode":null,"errorMessage":"Page %s empty","messagePattern":"Page (.+?) empty","errorType":"http","errorClass":"Http404","httpStatus":404,"severity":"warning","filePath":"django/contrib/sitemaps/views.py","lineNumber":125,"sourceCode":"        maps = sitemaps.values()\n    page = request.GET.get(\"p\", 1)\n\n    lastmod = None\n    all_sites_lastmod = True\n    urls = []\n    for site in maps:\n        try:\n            if callable(site):\n                site = site()\n            urls.extend(site.get_urls(page=page, site=req_site, protocol=req_protocol))\n            if all_sites_lastmod:\n                site_lastmod = getattr(site, \"latest_lastmod\", None)\n                if site_lastmod is not None:\n                    lastmod = _get_latest_lastmod(lastmod, site_lastmod)\n                else:\n                    all_sites_lastmod = False\n        except EmptyPage:\n            raise Http404(\"Page %s empty\" % page)\n        except PageNotAnInteger:\n            raise Http404(\"No page '%s'\" % page)\n    # If lastmod is defined for all sites, set header so as\n    # ConditionalGetMiddleware is able to send 304 NOT MODIFIED\n    if all_sites_lastmod:\n        headers = {\"Last-Modified\": http_date(lastmod.timestamp())} if lastmod else None\n    else:\n        headers = None\n    return TemplateResponse(\n        request,\n        template_name,\n        {\"urlset\": urls},\n        content_type=content_type,\n        headers=headers,\n    )\n","sourceCodeStart":107,"sourceCodeEnd":141,"githubUrl":"https://github.com/django/django/blob/ae25a40be07e8a749edf526df37c93e59d4a22c9/django/contrib/sitemaps/views.py#L107-L141","documentation":"Raised as Http404 when the paginator for a sitemap section raises `EmptyPage`, i.e. the requested `?p=N` query parameter exceeds the number of available pages. The sitemap view paginates each section's URLs, so requesting a page beyond the last produces a 404 instead of an empty list.","triggerScenarios":"A GET to a sitemap section URL with ?p=N where N is greater than site.paginator.num_pages, or N references a page that has no items because the queryset shrank since the index was generated.","commonSituations":"Search engines following cached links to old pages after content was deleted; manual testing with arbitrary ?p= values; a section whose queryset became empty so even page 1 is empty.","solutions":["Verify the requested page number is within site.paginator.num_pages (start at p=1).","Ensure the section's queryset/items() actually returns at least one page of results.","Regenerate and resubmit the sitemap index so crawlers only see valid page numbers."],"exampleFix":"// before - request /sitemap.xml?section=blog&p=99 when only 3 pages exist -> 404 'Page 99 empty'\n\n// after - link only valid pages\nfor page in range(1, sitemap.paginator.num_pages + 1):\n    url = f\"/sitemap.xml?section=blog&p={page}\"","handlingStrategy":"validation","validationCode":"def valid_page(p, num_pages):\n    return p.isdigit() and 1 <= int(p) <= num_pages\n\n# before building a sitemap URL\npage = request.GET.get('p', '1')\nif not valid_page(page, site.paginator.num_pages):\n    raise Http404('Page %s empty' % page)","typeGuard":"def page_within_bounds(p, paginator) -> bool:\n    try:\n        n = int(p)\n    except (TypeError, ValueError):\n        return False\n    return 1 <= n <= paginator.num_pages","tryCatchPattern":"from django.core.paginator import EmptyPage\nfrom django.http import Http404\n\ntry:\n    urls = site.get_urls(page=page, site=req_site, protocol=req_protocol)\nexcept EmptyPage:\n    raise Http404('Page %s empty' % page)","preventionTips":["Derive page numbers from paginator.num_pages rather than hardcoding.","Resubmit the sitemap index to search consoles after content changes.","Treat out-of-range page 404s as expected bot traffic, not bugs."],"tags":["django","sitemaps","http404","pagination","seo"],"analyzedSha":"ae25a40be07e8a749edf526df37c93e59d4a22c9","analyzedAt":"2026-08-06T21:46:51.801Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}