djangowagtailwagtail-streamfield

In Wagtail how can I access the Page object from inside the StructBlock template?


I have a page called Event For the event I would like to have a program. Program consists of ProgramEntry objects linked to the page with ParentalKey. I would like this program to be displayed in the html, but it should be in a streamfield so that the editors can place it wherever they like in the page. To do this I would like to render the my custom ProgramEntryBlock that is simply this:

class ProgramEntryBlock(blocks.StructBlock):
    class Meta:
        label = _("Program entry")
        template = "blocks/program-entry-block.html"

in my template blocks/program-entry-block.html I would like to have something like this:

{% if page.program_entries.exists %}
    (...)

    {% for entry in page.program_entries.all %}
        (...)
    {% endfor %}
{% endif %}

How can I pass the Page object in this template? Or, what could be a better solution to my problem?


Solution

  • I found out that in this section of the wagtail documentation it says we can do this:

    {% include_block page.body with page=page %}
    
    

    Inside the page template. Just like with django include tag.

    This solves my problem but I'm not sure if it is the best way to do it.