【问题标题】:Incorrect for loop solution in django appdjango 应用程序中的 for 循环解决方案不正确
【发布时间】:2015-03-16 08:13:34
【问题描述】:

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

【问题讨论】:

    标签: python django templates for-loop


    【解决方案1】:

    使用divisibleby 模板过滤器:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 2021-03-15
      • 2021-04-03
      • 2017-04-27
      • 2013-12-27
      • 1970-01-01
      相关资源
      最近更新 更多