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

<pre><code>class Node():
  name = "node name"
  children = []
</code></pre>

<p>There will be some object <code>root</code> that is a <code>Node</code>, and <code>children</code> is a list of <code>Node</code>s. <code>root</code> will be passed in the content of the template.</p>

<p>I have found <a href="http://blog.elsdoerfer.name/2008/01/22/recursion-in-django-templates/" rel="noreferrer">this</a> one discussion of how this might be achieved, but the poster suggests this might not be good in a production environment.</p>

<p>Does anybody know of a better way?</p>

I think the canonical answer is: "Don't".
What you should probably do instead is unravel the thing in your
view
code, so it's just a matter of iterating over (in|de)dents in the template. I think I'd do it by appending indents and dedents to a list while recursing through the tree and then sending that "travelogue" list to the template. (the template would then insert
```
<li>
```
and
```
</li>
```
from that list, creating the recursive structure with "understanding" it.)
I'm also pretty sure recursively including template files is really a
wrong
way to do it...