We have upgrade from wagtail 2.15.6 to 4.1.5 custom sitemap is failing
有以下错误
文件“./venv/lib/python3.9/site-packages/wagtail/contrib/sitemaps/sitemap_generator.py”,第 44 行,在 _urls 对于 self.paginator.page(page).object_list.iterator() 中的项目:
self.paginator.page(page).object_list 确实是一个列表
我们在urls.py中有以下内容
from wagtail.contrib.sitemaps.views import sitemap as wagtail_sitemap
available_sitemaps = {
'cms-pages': views.CMSPagesSitemap,
'static': views.StaticViewSitemap,
}
path(
'sitemap.xml',
wagtail_sitemap,
{'sitemaps': available_sitemaps},
name='sitemap',
),
我们的观点是
CMSPagesSitemap 类:
from wagtail.contrib.sitemaps import Sitemap as WagtailSitemap
class CMSPagesSitemap(WagtailSitemap):
"""Extend the default Wagtail sitemap generator to skip over pages
which use our custom authentication-required mixin.
"""
def items(self) -> list:
items_qs = super().items()
# Unfortunately we have to do some filtering in code, not at the DB.
# It's fine for the return value to be an iterable, not a QuerySet:
# https://docs.djangoproject.com/en/3.2/ref/contrib/sitemaps/#sitemap-class-reference
items = [instance for instance in items_qs if AuthenticatedUserRequired not in instance.__class__.mro()]
return items
StaticViewSitemap 类
class StaticViewSitemap(WagtailSitemap):
"""A manually curated section of the sitemap, focused
on Django-rendered views we want search engines to know about"""
changefreq = 'daily'
CONTACT_PAGE_PLACEHOLDER = 'CONTACT_PAGE_PLACEHOLDER'
def items(self):
# At the moment, these are deliberately only for unauthenticated pages.
import pdb
pdb;pdb.set_trace()
url_names = [
# TODO: replace pre-resolved URLs, above, with the proper contact view URL names
# when they have been ported from V1 into V2
'/contact/',
'/contact/events/',
'/contact/defence-and-security-organisation/',
'/contact/export-advice/',
'/contact/feedback/',
'/contact/domestic/',
'/contact/domestic/enquiries/',
'/contact/international/',
'/contact/selling-online-overseas/',
'/contact/selling-online-overseas/organisation/',
'/contact/office-finder/',
# These were removed from the V1 sitemap because the pages were 404ing anyway because
# FEATURE_EXPORTING_TO_UK_ON_ENABLED was not set on production any more, so the views
# ExportingToUKDERAFormView, ExportingToUKBEISFormView and ExportingToUKFormView have
# NOT YET been ported to Great V2
# '/contact/department-for-business-energy-and-industrial-strategy/',
# '/contact/department-for-environment-food-and-rural-affairs/',
# '/contact/exporting-to-the-uk/',
# '/contact/exporting-to-the-uk/import-controls/',
# '/contact/exporting-to-the-uk/other/',
# '/contact/exporting-to-the-uk/trade-with-uk-app/',
# The following are all auto-generated from the urlconf
'core:cookie-preferences',
'core:login',
'core:signup',
'core:robots',
'domestic:get-finance',
'domestic:uk-export-finance-lead-generation-form', # See location(), below
'domestic:project-finance',
'domestic:how-we-assess-your-project',
'domestic:what-we-offer-you',
'domestic:country-cover',
'domestic:uk-export-contact',
'domestic:market-access',
'domestic:report-ma-barrier', # See location(), below
'search:search',
'search:feedback',
]
return url_names
def changefreq(self, item):
# The Django-rendered pages don't change very often and we can always request
# a re-crawl if we have something we edit that we want to get out there ASAP.
# 'Monthly' seems like a reasonable middle ground, even though it might not be
# respected by search engines anyway
return 'monthly'
def location(self, item):
if item.startswith('/'):
return item
elif item == 'domestic:uk-export-finance-lead-generation-form':
return reverse(item, kwargs={'step': 'contact'})
elif item == 'domestic:report-ma-barrier':
return reverse(item, kwargs={'step': 'about'})
return reverse(item)