Django while loop

I wonder if there is a way to make a while loop in django (I think that is what I need)?

What I'm trying to do is list n / a ul / li.

The list is generated by a for loop in a for loop. But since some elements in the second loop have more children, I want to iterate or them before and so on until all child nodes are iterated. The only way I've found so far is to have a different loop. But this does not seem to be general and rather repetitive. And I need to know how many โ€œlevelsโ€ a child has.

Now it looks like this:

<ul> {% for item in items %} <li> {{ item.name }} {% if item.childs %} <ul> {% for child in item.childs %} <li>{{ child.name }}</li> {% endfor %} </ul> {% endif %} </li> {% endfor %} </ul> 

Or is there a smarter way to send data to a template? Is it possible to do this with some for / while loop?

.. Fredrick

+2
source share
3 answers

Turn the list into an inclusion tag , then include it in yourself.

+3
source

Looks like recursion can solve your problem if you want to delve into the "unknown" depth of children? There are quite a few posts about this on t'internet if you are looking for ...

+1
source

Source: https://habr.com/ru/post/1342765/


All Articles