【问题标题】:How to format index pagination url format django如何格式化索引分页url格式django
【发布时间】: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'),

【问题讨论】:

    标签: python django xml


    【解决方案1】:

    答案是覆盖通用站点地图函数,在该函数中添加参数页面,删除此列

    page = request.GET.get("p", 1)
    

    然后在 urls.py 的地方

     path('sitemap-<section>-<page>.xml', sitemaps.sitemap, {'sitemaps': sitemaps_pages}),
    

    在您的另一个站点地图网址上方

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 2017-02-15
      • 1970-01-01
      • 2016-03-10
      • 1970-01-01
      • 2012-06-28
      相关资源
      最近更新 更多