Search code examples
pythondjangodjango-templatesdjango-template-filterstemplatetags

How to nest or jointly use two template tags in Django templates?


I'm trying to use template filters to do run a loop, but I'm unable to combine two python statements within the same statement/template. Whats the correct way to combine two variables in a template? Please see the syntax and explanation below:

I'm building a forum with a double index, meaning, I've got a col-md-2 with list of categories. Each categories has forums, and depending on which category is clicked, that category's forums populate the next col-md-2. The remaining col-md-8 gets its content based on which category and which forum is selected.

My logic:

I've defined a template tag that loads the list of categories, which never change irrespective of which page gets loaded or which category or forum is selected. So that works fine. But based on the selected category, my second column needs to get populated. For this, I'm trying to define a custom filter (below). However, I'm not sure how to use this as it needs to be passed to another template where it runs a loop to render the html. Even if I create the for loop in this template (rather than passing it to another), I still have to do nested template tags, something like: {% for forum in {{ forum.category|forumindexlistbycategory }} %} In either cases, I get an error of the type Invalid filter: 'forumindexlistbycategory' or "with" in u'include' tag needs at least one keyword argument.

I've defined the following custom template filter in my pybb_tags.py:

from pybb.models import Forum

@register.filter
def forumindexlistbycat(category):
    forumlistbycat = Forum.objects.filter(category=category)
    return forumlistbycat

And in my template, I'm trying to load it as follows:

{% load i18n pybb_tags %}

<div class='category'>
    {% if category %}
    <h3>{{ category }}</h3>
    {% include 'pybb/forumindex_list.html' with forum_list=category.forums_accessed category=category parent_forum='' %}
    {% else %}
    <h3>{{ forum.category }}</h3>
    {% include 'pybb/forumindex_list.html' with forum_list= %}{{ forum.category|forumindexlistbycategory }}
    {% endif %}
</div>

Solution

  • So you must first properly register template tag.

    from django import template
    from pybb.models import Forum
    
    register = template.Library()
    
    @register.filter
    def forumindexlistbycat(category):
        forumlistbycat = Forum.objects.filter(category=category)
        return forumlistbycat
    

    Place code from above in file named as your filter, so forumindexlistbycat.py and move this file to templatetags folder in your app. If you don't have this folder you must create it. Don't forget to add empty file __init__.py in your templatetags folder. And now you could use it in template, so:

    {% load i18n forumindexlistbycat %}
    

    When your templatetag is registered you load it by it's name. And then you use it like:

    {% include 'pybb/forumindex_list.html' with forum_list=forum.category|forumindexlistbycategory %}
    

    Check for more info - Guide on Custom template tags and filters.