django app中for循环不正确的解决方法
Incorrect for loop solution in django app
我在 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"> </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>
使用 divisibleby
模板过滤器:
{% if forloop.counter|divisibleby:"5" %}
<div style="page-break-after: always;"></div>
{% endif %}
我在 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"> </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>
使用 divisibleby
模板过滤器:
{% if forloop.counter|divisibleby:"5" %}
<div style="page-break-after: always;"></div>
{% endif %}