【发布时间】:2021-10-13 01:21:00
【问题描述】:
问题和Django sitemap index pagination URL format真的很像 但我没有得到答案。 所以,我需要我的 url 像这样 /sitemap-products-3.xml,其中 3 是分页器的页面。我当前的工作链接:sitemap-products.xml?p=3。它是由通用函数(索引)生成的。我做了一些工作,链接看起来像 sitemap-products-3.xml ,但是在 urls.py 中,我不知道如何在分页器中传递页数,因为链接给了我 404。那是我的 sitemap.xml (索引函数)
def index(request, sitemaps, template_name='sitemap_index.xml', content_type='application/xml', sitemap_url_name='django.contrib.sitemaps.views.sitemap'):
req_protocol = request.scheme
req_site = get_current_site(request)
sitemaps_lastmod = []
sites = [] # all sections' sitemap URLs
for section, site in sitemaps.items():
# For each section label, add links of all pages of its sitemap
if callable(site):
site = site()
protocol = req_protocol if site.protocol is None else site.protocol
sitemap_url = reverse(sitemap_url_name, kwargs={'section': section})
absolute_url = '%s://%s%s' % (protocol, req_site.domain, sitemap_url)
sites.append(absolute_url)
sitemaps_lastmod.append(sitemaps[section].index_lastmod(sitemaps[section]))
# Add links to all pages of the sitemap.
for page in range(2, site.paginator.num_pages + 1):
absolute_url = absolute_url.strip('.xml')
sites.append('%s-%s.xml' % (absolute_url, page))
if len(sites) > len(sitemaps_lastmod):
for x in range(len(sites)-len(sitemaps_lastmod)):
sitemaps_lastmod.append(sitemaps[section].index_lastmod(sitemaps[section]))
sitez = zip(sites, sitemaps_lastmod)
return render(request, template_name, {'sitemaps': sitez}, content_type=content_type)
和 urls.py:
path('sitemap.xml', sitemaps.index, {'sitemaps': sitemaps_pages}),
path('sitemap-<section>.xml', sitemap, {'sitemaps': sitemaps_pages}, name='django.contrib.sitemaps.views.sitemap'),
【问题讨论】: