html – Wagtail: Method to programmatically build direct child url from current page-ThrowExceptions

Exception or error:

I am attempting to build a snippet based navigation bar. The links to be contained in this navigation bar all refer to child pages whose names will be the same in each instance of this snippet like so:

Parent
    Page1
    Page2
    Page3

I want each link to be built using the current page url it resides on combined with the child page like so /Path/to/Parent/Page1. The name of the Child pages will remain consistent but the Parent name pages will change. It has to be contained within a data snippet structure so that I can replicate it across what I am doing so I am building it in the html file like so:

{% extends "base.html" %}
{% load wagtailcore_tags %}
{% block content %}
    <p><a href="{% pageurl page.Page1 %}">About this project</a></p>
{% endblock %}

Specifically I don’t know how to format the href link to correctly refer to specific sub pages, from there I can extend this one link to all of the links to child pages I will be wanting to navigate in this way. Below is a copy of what exists in my models.py file relating to this section, I have followed the documentation/other posts and found the return page url function below but I do not know how to properly incorporate it.

@register_snippet
class Navbar(models.Model):
    url = models.URLField(null=True, blank=True)
    text = models.CharField(max_length=255)

    panels = [
        FieldPanel('url'),
        FieldPanel('text'),
    ]

    def __str__(self):
        return self.text


class PageUrl(Page):

    def returnPageUrl(self):
        return self.get_url()

Ideal Solution: Simple method of formatting the href links to refer to child pages which will have the same naming structure across all pages the snippet containing this structure is added in on.

EDIT: I found a solution which utilised the following structure in the href link:

<p><a href="././Page1">Page1</a></p>

Where each . referred to a previous parent level until it matched up to the child page I wanted. However given that this is dependent on my wagtail structure remaining the same if I could remove this dependency then I would still greatly appreciate an answer.

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *