Search code examples
pythondjangotemplatesdjango-mptt

How to get the depth level at a any given level in the template


Setup:

  • a tree structure of the models.
  • a representation of that structure in the HTML.

Issue:

How to know at any given recursion level the depth level at which currently I am?

Example:

This is the HTML:

<ul>
    <li>
        0

        <ul>
            <li>1</li>
            <li>1</li>
        </ul>
    </li>

    <li>0</li>
</ul>

0,1 are the children's depth level that I want to get at any given time.


Solution

  • You can use the template variable node.level and it will give you the indentation level.

    You can read more about it at:

    http://django-mptt.readthedocs.org/en/latest/technical_details.html?highlight=level#level