Custom Wagtail Sitemap
Asked Answered
H

1

6

I'm attempting to create a custom Wagtail sitemap that includes 'changefreq' and 'priority'. The default is just 'lastmod' and 'url'.

According to Wagtail docs (http://docs.wagtail.io/en/latest/reference/contrib/sitemaps.html) you can overwrite the default template by creating a sitemap at /wagtailsitemaps/sitemap.xml

I have done this. The sitemap template looks like this:

<?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>

I have added "wagtail.contrib.wagtailsitemaps", to my INSTALLED APPS in settings. I modified my Page class to include the get_sitemap_urls function, in an attempt to overwrite it.

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
            }
        ]

It's still not working. Am I missing something else? The Wagtail docs don't provide any more information and other documentation around the web on Wagtail is very light. Any help would be appreciated.

Handstand answered 17/6, 2016 at 16:10 Comment(0)
H
7

I figured it out. I had the function in the wrong class. It needed to go in each specific Page class to show up in the sitemap, not in the general BlockPage class. This also allowed me to set a different priority for each page if need be.

Solution:

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
            }
        ]
Handstand answered 17/6, 2016 at 19:6 Comment(2)
Probably because Stack Overflow editor is not too great, but the indentation is out. get_sitemap_urls is a class function (as indicated by self)Sankhya
@Sankhya Thanks for noticing. I fixed the indentation.Handstand

© 2022 - 2024 — McMap. All rights reserved.