Python Django模板:遍历列表

时间:2021-07-28 20:19:43

Technically it should iterate from 0 to rangeLength outputting the user name of the c[i][0].from_user...but from looking at example online, they seem to replace the brackets with dot notation. I have the following code:

从技术上讲,它应该从0迭代到rangeLength输出c[i][0].from_user的用户名……但是从在线的示例来看,它们似乎用点表示法替换了括号。我有以下代码:

<div id="right_pod">
{%for i in rangeLength%}
    <div class="user_pod" >
        {{c.i.0.from_user}}
    </div>
{% endfor %}

This currently outputs nothing :( If I replace "i" with 0...{{c.0.0.from_user}}...it will output something.. (the first user 10 times)

这当前不输出任何内容:(如果我用0. {c.0.0.from_user}替换“I”,它将输出一些东西…(第一个用户10次)

3 个解决方案

#1


16  

Do you need i to be an index? If not, see if the following code does what you're after:

你需要我做索引吗?如果没有,看看下面的代码是否实现了您想要的:

<div id="right_pod">
{% for i in c %}
    <div class="user_pod">
        {{ i.0.from_user }}
    </div>
{% endfor %}

#2


13  

Please read the entire documentation on the template language's for loops. First of all, that iteration (like in Python) is over objects, not indexes. Secondly, that within any for loop there is a forloop variable with two fields you'll be interested in:

请阅读模板语言中for循环的整个文档。首先,迭代(像Python一样)是针对对象而不是索引。其次,在任何for循环中都有一个forloop变量,它有两个你会感兴趣的字段:

Variable            Description
forloop.counter     The current iteration of the loop (1-indexed)
forloop.counter0    The current iteration of the loop (0-indexed)

#3


8  

You should use the slice template filter to achieve what you want:

您应该使用slice模板过滤器实现您想要的:

Iterate over the object (c in this case) like so:

遍历对象(本例为c)如下:

{% for c in objects|slice:":30" %}

This would make sure that you only iterate over the first 30 objects.

这将确保只对前30个对象进行迭代。

Also, you can use the forloop.counter object to keep track of which loop iteration you're on.

此外,还可以使用forloop。计数器对象以跟踪正在执行的循环迭代。

#1


16  

Do you need i to be an index? If not, see if the following code does what you're after:

你需要我做索引吗?如果没有,看看下面的代码是否实现了您想要的:

<div id="right_pod">
{% for i in c %}
    <div class="user_pod">
        {{ i.0.from_user }}
    </div>
{% endfor %}

#2


13  

Please read the entire documentation on the template language's for loops. First of all, that iteration (like in Python) is over objects, not indexes. Secondly, that within any for loop there is a forloop variable with two fields you'll be interested in:

请阅读模板语言中for循环的整个文档。首先,迭代(像Python一样)是针对对象而不是索引。其次,在任何for循环中都有一个forloop变量,它有两个你会感兴趣的字段:

Variable            Description
forloop.counter     The current iteration of the loop (1-indexed)
forloop.counter0    The current iteration of the loop (0-indexed)

#3


8  

You should use the slice template filter to achieve what you want:

您应该使用slice模板过滤器实现您想要的:

Iterate over the object (c in this case) like so:

遍历对象(本例为c)如下:

{% for c in objects|slice:":30" %}

This would make sure that you only iterate over the first 30 objects.

这将确保只对前30个对象进行迭代。

Also, you can use the forloop.counter object to keep track of which loop iteration you're on.

此外,还可以使用forloop。计数器对象以跟踪正在执行的循环迭代。