如何在索引sitemap中添加lastmod字段



我需要添加lastmod属性到sitemap索引。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'),

sitemaps.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

如何解决这个问题?

我做了一些研究,并修改了通用函数索引:

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文件到我的模板。然后添加到每个sitemap类index_lastmod函数,返回最后一个对象的更新时间在sitemap_index.xml中,我通过压缩的站点位置和站点lastmod进行迭代。sitemap_index.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])

最新更新