Aidan Daniel
Aidan Daniel

Reputation: 90

Wagtail render page tree

I am trying to create a sitemap that a user can use to see the Hierarchy of the site, child pages and parent pages etc but I can't seem to find a way of showing this. Is there an app I can use or will I need to write a custom function?

Upvotes: 1

Views: 903

Answers (1)

allcaps
allcaps

Reputation: 11228

You can get published pages like this:

Page.objects.live()

https://docs.wagtail.io/en/v0.7/core_components/pages/advanced_topics/queryset_methods.html#module-wagtail.wagtailcore.query

The default order is by path. Use the depth field (a number) and some css to create hierarchy.

You can add context to your page like this:

class SitemapPage(Page):
    ...

    def get_context(self, request, *args, **kwargs):
        context = super().get_context(request, *args, **kwargs)
        context.update({'pages': Page.objects.live()})
        return context

And in your template:

{% for obj in pages %}
   <a href="{% pageurl obj %}" class="level-{{ obj.depth }}">{{ obj.title }}</a>
{% endfor %}

Upvotes: 2

Related Questions