站点地图生成:手动更新 lastmod/last_published_at 页面
Sitemap Generation: Update lastmod/last_published_at of Page Manually
我有一个PublicationsPage
和一个PublicationPage
class。
PublicationsPage
显示其直接子项的列表,并将它们呈现在 http://mysite/publications
的简短预览列表中。
class PublicationsPage(Page):
# ...
def get_context(self, request):
context = super().get_context(request)
publications = PublicationPage.objects.child_of(self).live()
context['publications'] = publications
return context
这意味着只要有新的 PublicationPage
是 added/deleted/modified,列表就会相应地更新。但是由于我没有更新 PublicationsPage
,因此 /publications
位置的 lastmod
/last_published_at
属性永远不会改变。这不会误导搜索引擎吗?
每次我触摸子条目时都更新父页面的 last_published_at
日期。
class PublicationPage(Page):
# ...
def save(self, *args, **kwargs):
result = super().save(*args, **kwargs)
from datetime import datetime
parent_page = self.get_parent()
parent_page.last_published_at = datetime.now()
parent_page.save()
return result
还有其他建议吗?
生成 sitemap, you can set the property lastmod
时,它接受一个方法,为站点地图的每个项目调用。
因此,在生成 PublicationsPage
站点地图时,将 属性 设置为查询每个 PublicationsPage
项目的所有子项和 return 最新日期的方法。
您可以在页面模型上定义 get_sitemap_urls
,如下所示:
class PublicationsPage(Page):
def get_sitemap_urls(self, request=None):
# Get the defaults sitemap URLS.
urls = super().get_sitemap_urls(request=request)
# Get the last modifications time of your publications.
publications = PublicationPage.objects.child_of(self).live()
last_publication = publications.order_by('-last_published_at').first()
# Update the default entry.
if last_publication:
urls[0]['lastmod'] = last_publication.last_published_at
# Return the urls.
return urls
我有一个PublicationsPage
和一个PublicationPage
class。
PublicationsPage
显示其直接子项的列表,并将它们呈现在 http://mysite/publications
的简短预览列表中。
class PublicationsPage(Page):
# ...
def get_context(self, request):
context = super().get_context(request)
publications = PublicationPage.objects.child_of(self).live()
context['publications'] = publications
return context
这意味着只要有新的 PublicationPage
是 added/deleted/modified,列表就会相应地更新。但是由于我没有更新 PublicationsPage
,因此 /publications
位置的 lastmod
/last_published_at
属性永远不会改变。这不会误导搜索引擎吗?
每次我触摸子条目时都更新父页面的 last_published_at
日期。
class PublicationPage(Page):
# ...
def save(self, *args, **kwargs):
result = super().save(*args, **kwargs)
from datetime import datetime
parent_page = self.get_parent()
parent_page.last_published_at = datetime.now()
parent_page.save()
return result
还有其他建议吗?
生成 sitemap, you can set the property lastmod
时,它接受一个方法,为站点地图的每个项目调用。
因此,在生成 PublicationsPage
站点地图时,将 属性 设置为查询每个 PublicationsPage
项目的所有子项和 return 最新日期的方法。
您可以在页面模型上定义 get_sitemap_urls
,如下所示:
class PublicationsPage(Page):
def get_sitemap_urls(self, request=None):
# Get the defaults sitemap URLS.
urls = super().get_sitemap_urls(request=request)
# Get the last modifications time of your publications.
publications = PublicationPage.objects.child_of(self).live()
last_publication = publications.order_by('-last_published_at').first()
# Update the default entry.
if last_publication:
urls[0]['lastmod'] = last_publication.last_published_at
# Return the urls.
return urls