首页 > 解决方案 > django-templates 中的表中的 if 语句

问题描述

假设我们在模板中有表格

<table class="table">
          <thead>
            <tr>
            
             <th>Status</th>
            </tr>
          </thead>
          <tbody>
              {% for student in students %}
            <tr>
             
             {% if {{student.academic_status}}=="promoted" %}
             <td class=text-success>promoted</td>
             {% endif %}
            </tr>
            {% endfor %}
          </tbody>
        </table>

那么是否可以在django-templates的表中使用 if 语句

标签: pythondjangodjango-templates

解决方案


由于您已经在模板标签中,因此您不应该变量使用大括号 ( {{ … }}),因此:

{% if student.academic_status == "promoted" %}
    …
{% endif %}

推荐阅读