Tree view in django template
<p>I try to create simple TODOList app. Where you can create Project, then create tasks for project, subtasks for tasks and subtasks. I create a template to show task:</p>

<pre><code>&lt;li class='task'&gt;
&lt;div class="collapsible-header" id="task-name"&gt; {{task.title}}&lt;/div&gt;
&lt;div class="collapsible-body" data-task-pk='{{task.pk}}' id="task-details"&gt;
    {% include 'ProjectManager/views/control-block.html' %}
    &lt;p&gt;{{task.description}}&lt;/p&gt;
    &lt;ul class="collapsible popout" data-collapsible="expandable" id="subtasks"&gt;
        {% for sub_task in task.subtasks.all %}
           {% include "ProjectManager/views/task_view.html" with task=sub_task %}
        {% endfor %}
    &lt;/ul&gt;
&lt;/div&gt;
&lt;/li&gt;
</code></pre>

<p>You can see i try to create a list of subtask, by using this template recursively, but I got an error:</p>

<pre><code>'RecursionError' object has no attribute 'token'
</code></pre>

<p>I found some informations, that i should use variable to store template name, like this:</p>

<pre><code>&lt;li class='task'&gt;
&lt;div class="collapsible-header" id="task-name"&gt; {{task.title}}&lt;/div&gt;
&lt;div class="collapsible-body" data-task-pk='{{task.pk}}' id="task-details"&gt;
    {% include 'ProjectManager/views/control-block.html' %}
    &lt;p&gt;{{task.description}}&lt;/p&gt;
    &lt;ul class="collapsible popout" data-collapsible="expandable" id="subtasks"&gt;
        {% for sub_task in task.subtasks.all %}
            {% with node=sub_task template_name="ProjectManager/views/task_view.html" %}
                {% include template_name with task=node%}
            {% endwith %}
        {% endfor %}
    &lt;/ul&gt;
&lt;/div&gt;
&lt;/li&gt; 
</code></pre>

<p>I got an error:</p>

<pre><code>maximum recursion depth exceeded
</code></pre>

<p>But at start I wrote wrong:</p>

<pre><code>  {% with node=**subtask** template_name="ProjectManager/views/task_view.html" %}
</code></pre>

<p>And template display list of subtasks with empty elements (without task.title and description).</p>

<p>Then I tried to put some if condition:</p>

<pre><code>&lt;li class='task'&gt;
&lt;div class="collapsible-header" id="task-name"&gt; {{task.title}}&lt;/div&gt;
&lt;div class="collapsible-body" data-task-pk='{{task.pk}}' id="task-details"&gt;
    {% include 'ProjectManager/views/control-block.html' %}
    &lt;p&gt;{{task.description}}&lt;/p&gt;
    &lt;ul class="collapsible popout" data-collapsible="expandable" id="subtasks"&gt;
        {% if task.subtasks.all|length %}
            {% for sub_task in task.subtasks.all %}
                {% with node=sub_task template_name="ProjectManager/views/task_view.html" %}
                    {% include template_name with task=node%}
                {% endwith %}
            {% endfor %}
        {% endif %}
    &lt;/ul&gt;
&lt;/div&gt;
&lt;/li&gt;
</code></pre>

<p>But I got new error:</p>

<pre><code>maximum recursion depth exceeded while calling a Python object
</code></pre>

<p>How can I do this with Django templates? 
Full <a href="http://dpaste.com/0JKT0QF" rel="nofollow">traceback</a></p>

<p>In this way I show tasks list:</p>

<pre><code>&lt;div class="card-content"&gt;
    &lt;ul class="collapsible popout" data-collapsible="expandable" id="main-tasks"&gt;
    {% for task in project.tasks.all %}
        {% include 'ProjectManager/views/task_view.html' with task=task%}
    {%endfor%}
    &lt;/ul&gt;
&lt;/div&gt;
</code></pre>

<p>The <em>global</em> var's name is <code>task</code>. But your <em>local</em> var is <strong>also</strong> called <code>task</code>.</p>

<pre><code>{% for task in project.tasks.all %}
    {% include 'ProjectManager/views/task_view.html' with task=task%}
{%endfor%}
</code></pre>

<p>so i guess what you where <em>trying to do</em> is:</p>

<pre><code>{% for task_local in project.tasks.all %}
    {% include 'ProjectManager/views/task_view.html' with task_global_of_next_inheritance=task_local%}
{%endfor%}
</code></pre>

<p>but what happened is</p>

<pre><code>{% for task_local in project.tasks.all %}
    {% include 'ProjectManager/views/task_view.html' with task_global_of_next_inheritance=task_global%}
{%endfor%}
</code></pre>

<p>(using the global instead of the local var)
so you are just making the same call over and over again. if i am right, fix with</p>

<pre><code>{% for task_local in project.tasks.all %}
    {% include 'ProjectManager/views/task_view.html' with task=task_local%}
{%endfor%}
</code></pre>

python django django-templates