Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Wagtail Sitemap

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.

like image 365
kbdev Avatar asked Jun 17 '16 16:06

kbdev


1 Answers

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
            }
        ]
like image 89
kbdev Avatar answered Nov 15 '22 00:11

kbdev