在Django模板的树视图

时间:2016-09-27 12:13:19

标签: python django django-templates

我尝试创建简单的TODOList应用。您可以在哪里创建项目,然后为项目创建任务,为任务和子任务创建子任务。我创建了一个模板来显示任务:

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

你可以看到我试图通过递归使用这个模板创建一个子任务列表,但是我收到了一个错误:

'RecursionError' object has no attribute 'token'

我发现了一些信息,我应该使用变量来存储模板名称,如下所示:

<li class='task'>
<div class="collapsible-header" id="task-name"> {{task.title}}</div>
<div class="collapsible-body" data-task-pk='{{task.pk}}' id="task-details">
    {% include 'ProjectManager/views/control-block.html' %}
    <p>{{task.description}}</p>
    <ul class="collapsible popout" data-collapsible="expandable" id="subtasks">
        {% 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 %}
    </ul>
</div>
</li> 

我收到了一个错误:

maximum recursion depth exceeded

但是一开始我写错了:

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

模板显示带有空元素的子任务列表(没有task.title和description)。

然后我试着放一些if条件:

<li class='task'>
<div class="collapsible-header" id="task-name"> {{task.title}}</div>
<div class="collapsible-body" data-task-pk='{{task.pk}}' id="task-details">
    {% include 'ProjectManager/views/control-block.html' %}
    <p>{{task.description}}</p>
    <ul class="collapsible popout" data-collapsible="expandable" id="subtasks">
        {% 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 %}
    </ul>
</div>
</li>

但我收到了新的错误:

maximum recursion depth exceeded while calling a Python object

如何使用Django模板执行此操作? 完整traceback

通过这种方式,我展示了任务列表:

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

1 个答案:

答案 0 :(得分:1)

全球 var的名称为expected 'document.querySelectorAll(".line-1 .af-nb-follower")' to contain /[0-9]+/, but it was "" 。但您的本地 var 称为task

task

所以我猜你试图做什么是:

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

但发生了什么

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

(使用全局而不是本地var) 所以你只是一遍又一遍地打同一个电话。如果我是对的,请修复

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