Django while循环 [英] Django while loop

查看:315
本文介绍了Django while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在django中有没有办法做一个循环(我认为这是我以后的)?

I wonder if there's any way to do a while loop in django (I think that's what I'm after)?

我要做的是一个嵌套的ul / li列表。

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

该列表由for循环中的for循环生成。但是由于第二个for循环中的某些元素有更多的子节点,我想要遍历它们,等等,直到所有子节点被迭代出来。到目前为止我发现的唯一办法就是要有另一个循环。但这似乎不是通用的和相当重复的。而且我需要知道孩子们有多少级别。

The list is generated by a for loop in a for loop. But since some elements in the second for loop has more child's I want to iterate or them to and so on until all child nodes are iterated out. Only way I found so far is to have another for loop. But this seems not to generic and quite repetitive. And I need to know how many "levels" of child's there are.

这是现在的样子:

<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>

还是有更智能的方式将数据发送到模板?
可以使用某种for / while循环吗?

Or is there a smarter way to send the data to the template? Can one do this with some kind of for/while loop?

.. fredrik

..fredrik

推荐答案

将列表转成包含标签,然后包含它。

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

这篇关于Django while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆