【问题标题】:Custom Wagtail Sitemap自定义鹡鸰站点地图
【发布时间】:2016-06-17 16:10:48
【问题描述】:

我正在尝试创建一个包含“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 上的网络上的其他文档非常简单。任何帮助将不胜感激。

【问题讨论】:

    标签: django sitemap wagtail


    【解决方案1】:

    我想通了。我在错误的课程中使用了该功能。它需要进入每个特定的 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
                }
            ]
    

    【讨论】:

    • 可能是因为 Stack Overflow 编辑器不是太好,而是缩进了。 get_sitemap_urls 是一个类函数(由self表示)
    • @MagicLAMP 感谢您的关注。我修复了缩进。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    相关资源
    最近更新 更多