首页 > 解决方案 > 站点地图生成:手动更新页面的 lastmod/last_published_at

问题描述

我有PublicationsPagePublicationPage堂课。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添加/删除/修改新的列表时,列表都会相应地更新。但是由于我没有更新位置PublicationsPagelastmod/last_published_at属性,所以/publications永远不会改变。这不会误导搜索引擎吗?


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

还有其他建议吗?

标签: pythondjangositemapwagtail

解决方案


生成站点地图时,您可以设置属性lastmod,它接受一个方法,为站点地图的每个项目调用。

因此,在生成PublicationsPage站点地图时,将该属性设置为查询每个项目的所有子PublicationsPage项并返回最新日期的方法。


推荐阅读