Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I render a tree structure (recursive) using a django template?

Tags:

python

django

I have a tree structure in memory that I would like to render in HTML using a Django template.

class Node():   name = "node name"   children = [] 

There will be some object root that is a Node, and children is a list of Nodes. root will be passed in the content of the template.

I have found this one discussion of how this might be achieved, but the poster suggests this might not be good in a production environment.

Does anybody know of a better way?

like image 289
David Sykes Avatar asked Aug 28 '08 11:08

David Sykes


People also ask

What is template rendering in Django?

Rendering means interpolating the template with context data and returning the resulting string. The Django template language is Django's own template system. Until Django 1.8 it was the only built-in option available. It's a good template library even though it's fairly opinionated and sports a few idiosyncrasies.

What is a more efficient way to pass variables from template to view in Django?

POST form (your current approach) This answer is perfect and I learned a great deal!

Can I use Django template variable in JavaScript?

As of Django 2.1, a new built in template tag has been introduced specifically for this use case: json_script . The new tag will safely serialize template values and protects against XSS. Django docs excerpt: Safely outputs a Python object as JSON, wrapped in a tag, ready for use with JavaScript.

How does template inheritance work in Django?

According to Django, “Template inheritance allows you to build a base “skeleton” template that contains all the common elements of your site and defines blocks that child templates can override.” Let's say that we have a website which comprises of two pages — the 'home' page and a 'contact' page.


2 Answers

Using with template tag, I could do tree/recursive list.

Sample code:

main template: assuming 'all_root_elems' is list of one or more root of tree

<ul> {%for node in all_root_elems %}      {%include "tree_view_template.html" %} {%endfor%} </ul> 

tree_view_template.html renders the nested ul, li and uses node template variable as below:

<li> {{node.name}}     {%if node.has_childs %}         <ul>          {%for ch in node.all_childs %}               {%with node=ch template_name="tree_view_template.html" %}                    {%include template_name%}               {%endwith%}          {%endfor%}          </ul>     {%endif%} </li> 
like image 94
Rohan Avatar answered Oct 20 '22 05:10

Rohan


I'm too late.
All of you use so much unnecessary with tags, this is how I do recursive:

In the "main" template:

<!-- lets say that menu_list is already defined --> <ul>     {% include "menu.html" %} </ul> 

Then in menu.html:

{% for menu in menu_list %}     <li>         {{ menu.name }}         {% if menu.submenus|length %}             <ul>                 {% include "menu.html" with menu_list=menu.submenus %}             </ul>         {% endif %}     </li> {% endfor %} 
like image 39
Arthur Sult Avatar answered Oct 20 '22 05:10

Arthur Sult