自定义Wa尾网站地图

kbd*_*dev 4 sitemap django wagtail

我正在尝试创建包含“ 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>
Run Code Online (Sandbox Code Playgroud)

我在设置中的“已安装的应用程序”中添加了“ 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
        }
    ]
Run Code Online (Sandbox Code Playgroud)

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

kbd*_*dev 5

我想到了。我在错误的课堂上听课了。它需要进入每个特定的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
            }
        ]
Run Code Online (Sandbox Code Playgroud)