【问题标题】:how to add lastmod field in index sitemap django如何在索引站点地图django中添加lastmod字段
【发布时间】:2021-10-10 05:48:02
【问题描述】:

我需要将 lastmod 属性添加到站点地图索引。 它看起来像 django.contrib.sitemaps.views.index 虽然不包括 lastmode 这是我的 sitemap.xml:

<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
<sitemap>
<loc>localhost:8000/sitemap-pages.xml</loc>
</sitemap>
</sitemapindex>

我的 urls.py

sitemaps_pages = {

    'pages': sitemaps.PageViewSitemap,
    'life': sitemaps.LifeViewSitemap,
    'lifes': sitemaps.LifesSitemap,
    'novosti': sitemaps.NewsViewSitemap,
    'novost': sitemaps.NewsSitemap,
    'catalog': sitemaps.CatalogViewSitemap,
    'categories': sitemaps.CategorySitemap,
    'regions': sitemaps.RegionSitemap,
    'times': sitemaps.TimeSitemap,
    'material': sitemaps.MaterialSitemap,
    'products': sitemaps.ProductsSitemap,
}
    path('sitemap-<section>.xml', sitemap, {'sitemaps': sitemaps_pages}, name='django.contrib.sitemaps.views.sitemap'),
    path('sitemap.xml', index, {'sitemaps': sitemaps_pages}, name='django.contrib.sitemaps.views.sitemap'),

站点地图.py

class ProductsSitemap(sitemaps.Sitemap):
    protocol = 'https'
    try:
        priority_filter = strip_tags(Info.objects.get(name='priority_filter').value)
        frequency_filter = strip_tags(Info.objects.get(name='frequency_filter').value)
    except Exception:

        priority_filter = '0.5'
        frequency_filter = 'daily'
    priority = priority_filter
    changefreq = frequency_filter
    limit = 1000

    def items(self):
        return Product.objects.all()

    def location(self, item):
        return str(item.url)

    def lastmod(self, item):
        if item.url == '/':
            return Product.objects.latest('updated_at').updated_at
        else:
            return item.updated_at

我该如何解决这个问题?

【问题讨论】:

    标签: python django xml web sitemap


    【解决方案1】:

    我做了一些研究,修改了泛型函数索引:

    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):
                sites.append('%s?p=%s' % (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)
    

    我还将 sitemap_index.xml 文件复制到我的模板中。然后添加到每个站点地图类 index_lastmod 函数,返回最后一个对象的更新时间 在 sitemap_index.xml 中,我正在遍历压缩站点 locationg 和站点 lastmod。 站点地图索引.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    {% for location,lastmod in sitemaps %}
        <sitemap>
            <loc>{{ location }}</loc>
            <lastmod>{{ lastmod|date:"Y-m-d" }}</lastmod>
        </sitemap>
    {% endfor %}
    </sitemapindex>
    

    在这里,我正在检查站点的位置是否比站点 lastmod 更多(因为分页), 然后只需复制最后一个元素的 lastmod

    if len(sites) > len(sitemaps_lastmod):
                for x in range(len(sites)-len(sitemaps_lastmod)):
                    sitemaps_lastmod.append(sitemaps[section].index_lastmod(sitemaps[section])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-19
      • 1970-01-01
      相关资源
      最近更新 更多