Django无效的块标记:endelse和ifeq

2024-03-29 15:40:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用djangoifequalelse标记来判断变量是否等于80或{}。所以,这是代码:

{% if firewalls %}
<thead>
  <tr>
    <th>IP address</th>
    <th>Function</th>
  </tr>
</thead>
{% endif %}
<tbody>
{% for firewall in firewalls %}
  <tr>
    <td>{{ host_ip }} : {{ firewall.from_port }}</td>

    {% ifequal firewall.to_port 22 %} <td>Ssh Service</td>
    {% ifequal firewall.to_port 80 %} <td>Web Service</td>
    {% else %} <td>Unknown Service</td>{% endifequal %}{% endelse %}

  </tr>
{% endfor %}

错误是Invalid block tag: 'endelse', expected 'else' or 'endifequal'。有人能帮我吗?谢谢!在


Tags: to标记portserviceelsetrtdfirewall
3条回答

Django支持相等运算符。为了解决您的代码问题,我会使用:

{% if firewalls %}
<thead>
  <tr>
    <th>IP address</th>
    <th>Function</th>
  </tr>
</thead>
{% endif %}
<tbody>
{% for firewall in firewalls %}
  <tr>
    <td>{{ host_ip }} : {{ firewall.from_port }}</td>

    {% if firewall.to_port==22 %} <td>Ssh Service</td>
    {% elif firewall.to_port==80 %} <td>Web Service</td>
    {% else %} <td>Unknown Service</td>{% endif %}

  </tr>
{% endfor %}
    {% ifequal firewall.to_port 22 %} <td>Ssh Service</td>{% endifequal %}
    {% ifequal firewall.to_port 80 %} <td>Web Service</td>{% endifequal %}
    {% if firewall.to_port !=22 and if firewall.to_port !=80   %} <td>Unknown Service</td>{% endif %}

每一个ifequal你都需要用endifequal来关闭它,你错过了一个

ifequal标记的替代方法是使用if标记和==接线员。弓“==”运算符的幂:

{% if firewall.to_port == 20 %}
   <td>Ssh Service</td>
{% elif firewall.to_port == 80 %}
   <td>Web Service</td>
{% else %}
   <td>Unknown Service</td>
{% endif %}

这种方法还可以节省代码处理时间,因为它不会为每个端口号计算all if条件。在

相关问题 更多 >