Django: Display Nested Tree Structure with Recursion
March 3, 2015
Description
Below is how we used the Django template system to render nested list items. Since there could potentially be an infinite number of levels, the best option was recursion. I’m not going to get into the discussion of whether or not you should be doing this in a view, but this is how we implemented the solution…
Code
Task_List Visual
Explanation
The template code resides in the “todo/task_item.html” file, and functions as follows:
- Code iterates through all of the task items in task_list.
- Outputs each task and associated layout.
- If the task has children:
- Set the task_list variable as the children of the current task.
- Recursive call to current template file.
- Output any nested children in a nested list.
- Repeat recursion as necessary.