5

我正在尝试创建一个包含“changefreq”和“priority”的自定义 Wagtail 站点地图。默认只有“lastmod”和“url”。

根据 Wagtail 文档(http://docs.wagtail.io/en/latest/reference/contrib/sitemaps.html),您可以通过在 /wagtailsitemaps/sitemap.xml 创建站点地图来覆盖默认模板

我已经做到了。站点地图模板如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% spaceless %}
{% for url in urlset %}
  <url>
    <loc>{{ url.location }}</loc>
    {% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}   </lastmod>{% endif %}
    {% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %}
    {% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %}
   </url>
{% endfor %}
{% endspaceless %}
</urlset>

我在设置中的安装应用程序中添加了“wagtail.contrib.wagtailsitemaps”。我修改了我的 Page 类以包含 get_sitemap_urls 函数,试图覆盖它。

class BlockPage(Page):
    author = models.CharField(max_length=255)
    date = models.DateField("Post date")
    body = StreamField([
        ('heading', blocks.CharBlock(classname='full title')),
        ('paragraph', blocks.RichTextBlock()),
        ('html', blocks.RawHTMLBlock()),
        ('image', ImageChooserBlock()),
    ])

    search_fields = Page.search_fields + (
        index.SearchField('heading', partial_match=True),
        index.SearchField('paragraph', partial_match=True),
    )

    content_panels = Page.content_panels + [
        FieldPanel('author'),
        FieldPanel('date'),
        StreamFieldPanel('body'),
    ]

    def get_sitemap_urls(self):
        return [
            {
                'location': self.full_url,
                'lastmod': self.latest_revision_created_at,
                'changefreq': 'monthly',
                'priority': .5
            }
        ]

它仍然无法正常工作。我还缺少其他东西吗?Wagtail 文档没有提供更多信息,Wagtail 上的网络上的其他文档非常简单。任何帮助,将不胜感激。

4

1 回答 1

7

我想到了。我在错误的课程中使用了该功能。它需要进入每个特定的 Page 类才能显示在站点地图中,而不是一般的 BlockPage 类中。如果需要,这也允许我为每个页面设置不同的优先级。

解决方案:

class HomePage(Page):
    body = RichTextField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('body', classname='full')
    ]

    def get_sitemap_urls(self):
        return [
            {
                'location': self.full_url,
                'lastmod': self.latest_revision_created_at,
                'changefreq': 'monthly',
                'priority': 1
            }
        ]

class AboutPage(Page):
    body = RichTextField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('body', classname='full')
    ]

    def get_sitemap_urls(self):
        return [
            {
                'location': self.full_url,
                'lastmod': self.latest_revision_created_at,
                'changefreq': 'monthly',
                'priority': .5
            }
        ]
于 2016-06-17T19:06:34.800 回答