【问题标题】:Getting the next item in a loop获取循环中的下一项
【发布时间】:2011-12-22 18:19:14
【问题描述】:

我想做的很简单:我有一个模型,我正在运行一个重组循环。现在我想将循环的前 4 项并排显示,然后在下一行继续显示接下来的 4 项,直到列表完成。如果没有更多项目,则该行中的其他单元格可以保持空白。

我试图了解如何在这里使用迭代,但无法弄清楚它如何适合我所拥有的。另外,我认为应该可以以更简单的方式执行此操作...下面我复制了我现在拥有的代码,这显然显示了循环的第一项 4 次,然后是第二项的 4 次,等等。

我知道这个网站上有一些类似的问题,但我无法利用它们为我的问题开发解决方案。我在 Google App Engine 上运行 Python。非常感谢任何帮助!

{% regroup communities|dictsort:"in_country" by in_country as community_list %}

{% for in_country in community_list %}
<h2 style="font-size:16px;">{{ in_country.grouper }}</h2>

<table cellspacing="0">
{% for item in in_country.list|dictsort:"name" %}

<tr style="text-align:center;">
 <td width="200px" class='community_table'>
    <img src="{{ item.image }}" style="height:40px;"><br />
    <a href='{{ item.url }}' style="font-size:10px; margin-left:10px;" TARGET = "_blank">{{ item.name }}</a><br />
    {{ item.com_type }}<br />
    {{ item.in_city }}<br />
 </td>

 <td width="200px" class='community_table'>
    <img src="{{ item.image }}" style="height:40px;"><br />
    <a href='{{ item.url }}' style="font-size:10px; margin-left:10px;" TARGET = "_blank">{{ item.name }}</a><br />
    {{ item.com_type }}<br />
    {{ item.in_city }}<br />
 </td>

 <td width="200px" class='community_table'>
    <img src="{{ item.image }}" style="height:40px;"><br />
    <a href='{{ item.url }}' style="font-size:10px; margin-left:10px;" TARGET = "_blank">{{ item.name }}</a><br />
    {{ item.com_type }}<br />
    {{ item.in_city }}<br />
 </td>

 <td width="200px" class='community_table'>
    <img src="{{ item.image }}" style="height:40px;"><br />
    <a href='{{ item.url }}' style="font-size:10px; margin-left:10px;" TARGET = "_blank">{{ item.name }}</a><br />
    {{ item.com_type }}<br />
    {{ item.in_city }}<br />
 </td>

{% endfor %}     
</table>  

{% endfor %}

【问题讨论】:

  • 这个问题与Python无关。
  • @DanielRoseman 不同意。该应用程序是用 Python 编写的,它使用 Python 模板引擎,解决方案可能是“在传递给模板之前在 Python 中进行分组”。

标签: python google-app-engine loops django-templates


【解决方案1】:
<table cellspacing="0">
{% for item in in_country.list|dictsort:"name" %}

{% if forloop.counter0|divisibleby:4 %}
<tr style="text-align:center;">
{% endif %}

 <td width="200px" class='community_table'>
    <img src="{{ item.image }}" style="height:40px;"><br />
    <a href='{{ item.url }}' style="font-size:10px; margin-left:10px;" TARGET = "_blank">{{ item.name }}</a><br />
    {{ item.com_type }}<br />
    {{ item.in_city }}<br />
 </td>

{% if forloop.counter|divisibleby:4 %}
 </tr>
{% endif %}

{% endfor %}
</table>

【讨论】:

  • 感谢您的回答。我明白你在做什么,它实际上比我的尝试更合乎逻辑。但是,在尝试代码时会出现错误: TemplateSyntaxError: Invalid filter: 'divisible_by' 顺便说一下,最后的'forloop.counter' 不应该也以 0 结尾(forloop.counter0)
  • 对不起,应该是divisibleby。不,您希望第一个标签仅出现在第 0、4、8 行等上,而最后一个标签出现在第 3、7、11 行等上。
  • 我想通了 :) 它是 divisibleby 而不是 divisible_by。其余的我保持原样。非常感谢! PS。写完以上内容后阅读您的评论
  • 看到问题和答案很有趣,因为我的用例类似:在列表中显示城市类型的项目。我还使用表格进行布局,使用 GAE + Jinja2 进行渲染,使用 django 的 .po 和 .mo 文件的 i18n 库进行本地化。感谢您分享您的做法
【解决方案2】:

我建议在将 Python 代码传递给模板之前对行进行分组。你可以使用这样的迭代器来做到这一点:

my_iter = iter(my_list)
grouped = zip(my_iter, my_iter, my_iter, my_iter) # or zip(*[my_iter]*4)

【讨论】:

  • 谢谢尼克。在发布问题之前,我查看了 iter 函数,但我不明白。我想我会回到那个文档:) 再次感谢!
猜你喜欢
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
  • 2019-05-06
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
  • 2011-07-03
  • 1970-01-01
相关资源
最近更新 更多