【问题标题】:Django template ifequal filter for forloop用于forloop的Django模板ifequal过滤器
【发布时间】:2012-02-23 09:14:33
【问题描述】:

我需要在 forloop 中创建一个过滤的 forloop。上下文是电子商务。我有产品类别,每个类别下都有各种产品。

要列出它们,我会这样做:

{% for c in categories %}
Products in {{ c.name }}:

{% for p in products %}

<table>
{% ifequal p.categoryname c.name %}
{% cycle '<tr>' '' '' %}
<td>{{ p.productname }}</td>
{% cycle '' '' '</tr>' %}
{% endifequal %}
</table>

{% endfor %}
{% endfor %}

我需要在每行 3 行的表格中列出产品。但是,循环浏览产品不会产生预期的效果,因为即使 ifequal 语句未满足,也会发生循环。是否有可能通过过滤器合并产品 forloop 和 ifequal 条件?

提前致谢!

【问题讨论】:

    标签: django filter for-loop cycle


    【解决方案1】:

    {% regroup %} 标签完成这项工作。

    【讨论】:

    • 我喜欢这个模板标签。我是前几天才知道的,从那以后我推荐了很多次!
    • 谢谢,正是我想要的!
    猜你喜欢
    • 2015-04-09
    • 2011-01-07
    • 1970-01-01
    • 2017-02-22
    • 2016-06-26
    • 2017-12-29
    • 2010-10-01
    • 2011-11-22
    • 1970-01-01
    相关资源
    最近更新 更多