我在Django项目中有一个for循环,我要做的是:
If
morning_recess == True
lunch_recess == True
afternoon_recess == True
then the bootstrap tag in that field should be
<td><span class="badge badge-success">Success</span></td>
else
<td> None </td>以下是我的当前代码:
<table style="width:100%">
<tr>
<th>Student Name</th>
<th>Morning Recess</th>
<th>Lunch Recess</th>
<th>Afternoon Recess</th>
<th>Earned At</th>
</tr>
<tr>
{% for i in students_recess_today %}
{% if i.morning_recess == True %}
<td>{{i.student_ps }}</td>
<td><span class="badge badge-success">Success</span></td>
<td>{{i.lunch_recess}}</td>
<td>{{i.afternoon_recess}}</td>
<td>{{i.created_at}}</td>
{% else %}
<td>{{i.student_ps }}</td>
<td>None</td>
<td>{{i.lunch_recess}}</td>
<td>{{i.afternoon_recess}}</td>
<td>{{i.created_at}}</td>
{% endif %}
</tr>
{% endfor %}
</table>
</div>morning_recess工作正常,但是如果我在下一条语句之后执行另一条if语句,那么我的表的顺序就会变得一团糟。我该如何正确地写这个?谢谢
发布于 2020-02-05 16:46:30
还不清楚“It”意味着什么,因为在您的示例中,在列之前有一个额外的列。但是您可以在模板中的任何地方放置{% if %}语句,例如:
<td>
{% if i.morning_recess %}
<span class="badge badge-success">Success</span>
{% else %}
<span>None</span>
{% endif %}
</td>
<td>
{% if i.lunch_recess %}
<span class="badge badge-success">Success</span>
{% else %}
<span>None</span>
{% endif %}
</td>
<td>
{% if i.afternoon_recess %}
<span class="badge badge-success">Success</span>
{% else %}
<span>None</span>
{% endif %}
</td>
...另外,正如其他评论者所建议的,for循环可能应该包装表(<tr>...</tr>)的行,而不是列(<td>)。
发布于 2020-02-05 16:39:32
在tr元素中有部分循环。它在开始时开始一行,对于每个学生都添加列和结束一行,因此它最终看起来像<tr></tr></tr></tr>。
您应该将for语句移出该行,如下所示:
...
{% for i in students_recess_today %}
<tr>
...
</tr>
{% endfor %}
</table>
</div>https://stackoverflow.com/questions/60080129
复制相似问题