2016-09-27 85 views
0

我尝试创建简单的TODOList应用程序。您可以在哪里创建项目,然后为项目创建任务,为任务和子任务创建子任务。我创建一个模板的任务,以显示:django模板中的树视图

<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 

但在我开始写错误:

​​

而且模板显示空元素(没有task.title和说明)子任务的列表。

然后我试图把一些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> 
+0

你可以添加视图的名称/路径?看起来像是一遍又一遍地被调用。你能分享错误引发的确切路线吗? –

+0

@ht_我写错了,正是我在浏览器中看到它们,完整回溯显示在链接中。你需要什么名字和路径? –

回答

0

全球变种的名字是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%} 

,但所发生的事情是

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

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

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

它的返回错误:'RecursionError'对象没有'token'属性 –

+0

我指的是第二个错误,“调用Python对象时超出最大递归深度” –

+0

如果我使用第二个列表,我得到:'超过最大递归深度 - 错误,第三个列表中,我得到了'调用Python对象时超出最大递归深度' - 错误 –