For循环HTML Django

For循环HTML Django,html,django,Html,Django,我在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>

我在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>
If
早晨休息==正确
午休时间==正确
下午课间休息==正确
那么该字段中的引导标记应该是
成功
其他的
没有一个
这是我目前的代码:

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


学名
上午休息
午休
下午休息
挣
{今天我在学生课间休息时的百分比%}
{%if i.morning_session==True%}
{{i.student_ps}
成功
{{i.午餐{U休息}
{{下午{休息}
{{i.created_at}}
{%else%}
{{i.student_ps}
没有一个
{{i.午餐{U休息}
{{下午{休息}
{{i.created_at}}
{%endif%}
{%endfor%}

早上的休息时间很好,但是如果我在下一个if语句之后再做一个if语句,我的桌子的顺序就会变得一团糟。我如何正确地写这个?谢谢

您的
tr
元素中有一部分循环。它从开始处开始一行,每个学生都会添加列和结束一行,因此它看起来像

您应该将
for
语句移到行外,如下所示:

。。。
{今天我在学生课间休息时的百分比%}
...
{%endfor%}
不清楚“在该字段中”是什么意思,因为在您的示例中,在
晨休
列之前有一个额外的列。但是您可以将
{%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>
...

{%if i.morning_recurse%}
成功
{%else%}
没有一个
{%endif%}
{%if i.午餐\休会%}
成功
{%else%}
没有一个
{%endif%}
{%如果我下午休息%}
成功
{%else%}
没有一个
{%endif%}
...

同样,正如其他评论者所建议的,for循环可能应该包装表中的行(
..
),而不是列(
)。

如果您想要多个表行,那么
是否应该在
{%for…}
循环中?否则,您需要澄清问题。还建议浏览器“查看页面源代码”,查看html实际生成的内容。生成一些
将有助于调试。