django app中的循环解决方案不正确

时间:2015-03-16 08:13:35

标签: python django templates for-loop

我在django应用程序中有一个包含对象的页面。我使用for循环显示它。如果循环处于5个循环,我想做类似的事情,我想要出现在新页面中。我需要这个来生成pdf文件。 在每个网站上,我想只有5个对象。但是下面的解决方案对我不起作用。

<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
    <h3>{% trans 'In progress' %}</h3>
    {% for worked_on in worker.worked_on.all %}
        <tr>
            <td width="33%" align="left" valign="top">&nbsp;</td>
            <td width="67%" class="project-name">
                <a href="#">{{ translated_project.title }}</a>
            </td>
        </tr>
        <tr>
            <td class="project-image" width="33%" align="left" valign="top">
                // CONTENT
            </td>
            <td class="project-description" valign="top">
                // CONTENT
            </td>
        </tr>
        <tr>
            <td>
                <p>
                {% if forloop.counter == 5 %}
                    <div style="page-break-after: always;"></div>
                {% endif %}
                </p>
            </td>
        </tr>
    {% endfor %}
</table>

1 个答案:

答案 0 :(得分:3)

使用divisibleby模板过滤器:

{% if forloop.counter|divisibleby:"5" %}
    <div style="page-break-after: always;"></div>
{% endif %}