How to nest or jointly use two template tags in Django templates?
<p>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:</p>

<p>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.</p>

<p>My logic:</p>

<p>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: <code>{% for forum in {{ forum.category|forumindexlistbycategory }} %}</code> In either cases, I get an error of the type <code>Invalid filter: 'forumindexlistbycategory'</code> or <code>"with" in u'include' tag needs at least one keyword argument</code>.</p>

<p>I've defined the following custom template filter in my pybb_tags.py:</p>

<pre><code>from pybb.models import Forum

@register.filter
def forumindexlistbycat(category):
    forumlistbycat = Forum.objects.filter(category=category)
    return forumlistbycat
</code></pre>

<p>And in my template, I'm trying to load it as follows:</p>

<pre><code>{% load i18n pybb_tags %}

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

<p>So you must first properly register template tag.</p>

<pre><code>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
</code></pre>

<p>Place code from above in file named as your filter, so <code>forumindexlistbycat.py</code> 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 <code>__init__.py</code> in your templatetags folder. And now you could use it in template, so:</p>

<pre><code>{% load i18n forumindexlistbycat %}
</code></pre>

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

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

<p>Check for more info - <a href="https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/" rel="nofollow">Guide on Custom template tags and filters.</a></p>

python django django-templates django-template-filters templatetags