2016-09-27 84 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對象時超出最大遞歸深度' - 錯誤 –