Skip to content

Instantly share code, notes, and snippets.

@VincentLoy
Last active June 19, 2024 16:52
Show Gist options
  • Save VincentLoy/e693aa1f149a59f465a5a71b6d937b9a to your computer and use it in GitHub Desktop.
Save VincentLoy/e693aa1f149a59f465a5a71b6d937b9a to your computer and use it in GitHub Desktop.
Easy snippet to get breadcrumb in a Wagtail page
<div class="breadcrumb-content">
{% if self.get_ancestors|length > 1 %}
<ul class="breadcrumb">
{% for p in self.get_ancestors %}
{% if p.is_root == False %}
<li><a href="{{ p.url }}">{{ p.title }}</a></li>
{% endif %}
{% endfor %}
<li class="active">{{ self.title }}</li>
</ul>
{% endif %}
</div>
@Nigel2392
Copy link

Nigel2392 commented Jun 19, 2024

@ismayil-ismayilov To be clear, you're not going from your browser to the page? IE the page.route method does not get invoked?

Sorry about confusion, it actually hits one time no matter the depth in my case. I am not sure what may cause such difference. It has to get > "parent" data from somewhere, either it has to be prefetched already and cached or it has to be requested.
How it would else be possible not to hit DB using get_ancestors()?

Get ancestors will for now always perform a query - it is not cached.

The parent objects however are cached upon routing to that page.

IE.
Homepage
> BlogPage
> > BlogIndexPage

You should be able to travel from BlogIndexPage to Homepage using get_parent without it performing any queries (inside the serve method of BlogIndexPage, after page.route has been invoked by the page serve view)

Can you tell me if this is the case for you?

@ismayil-ismayilov
Copy link

ismayil-ismayilov commented Jun 19, 2024

Unfortunately, I do not think that's a case for me. I strongly believe I caused a confusion since I may not have the same application method as you refer, sorry about it.

I used your Wagtail repo. It still hits DB.

This is my "simplified" Page class. I basically, applied your method to create a breadcrumb. Nothing more or less.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment