Charles Smith
Charles Smith

Reputation: 3289

Re-use Wagtail Index Template for Multiple Lists

I have several product pages on my site that will have identical index and page setup but a different url path. I would like to re-use the template but filter the results to only show the child objects of that index page.

For example:

Currently

www.../carnival - index page that displays all child objects
www.../carnival/rides-games - child page of carnival
www.../carnival/etc...

I want to use the same index page on other sections of the site:

www.../catering - index page that displays all child objects
www.../catering/fun-food - child page of catering
www.../catering/etc...

But, when I use the same index page and visit my carnival page, I see all my catering child objects as well.

Below is my code - please help me out; I know there has to be a DRY way of doing this. Thank you.

standard_index_page.html

{% block content %}
...
 {% standard_index_listing %}
...
{% endblock %}

standard_index_listing.html

{% if pages %}
  {% for pages in pages %}
    <div class="col-xs-6 col-sm-4 col-md-3 mt20 hover-float">
      <div class="team-two">
        {% if pages.feed_image %}
          {% image pages.feed_image original as img %}
          <div class="team-one" data-animation="zoomIn" data-animation-delay="100" style="background: url('{{ img.url }}') no-repeat top center; background-size: cover"></div>
        {% endif %}
        <h5>{{ pages.title }}</h5>
        <small><a href="{% pageurl pages %}" class="color-pasific">Learn More </a></small>
      </div>
    </div>
  {% endfor %}
{% endif %}

home_tags.py

@register.inclusion_tag(
    'home/tags/standard_index_listing.html',
    takes_context=True
)
def standard_index_listing(context):
    pages = StandardPage.objects.live()
    return {
        'pages': pages.select_related('feed_image'),
        'request': context['request'],
    }

Upvotes: 0

Views: 189

Answers (1)

gasman
gasman

Reputation: 25292

Within the context dictionary passed to the standard_index_listing tag, you have the current page available as 'page'. You can use this to filter the queryset (see http://docs.wagtail.io/en/v1.6.2/reference/pages/queryset_reference.html#module-wagtail.wagtailcore.query):

def standard_index_listing(context):
    pages = StandardPage.objects.live().child_of(context['page'])
    return {
        'pages': pages.select_related('feed_image'),
        'request': context['request'],
    }

Upvotes: 1

Related Questions