Django无效的代码块标记:endelse和ifequal [英] Django Invalid block tag: endelse and ifequal

查看:53
本文介绍了Django无效的代码块标记:endelse和ifequal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Django ifequal else 标记来判断变量是否等于 80 22 .所以,这是代码:

I want to use django ifequal and else tag to judge if a variable equals 80 or 22. So, this is code:

{% 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 %}

错误为无效的块标记:"endelse",预期的"else"或"endifequal" .有人可以帮我吗?非常感谢!

And the error is Invalid block tag: 'endelse', expected 'else' or 'endifequal'. Could someone helps me? Thanks a lot!

推荐答案

ifequal标记的替代方法是使用if标记和==运算符.赋予'=='运算符强大的力量:

An alternative to the ifequal tag is to use the if tag and the == operator.Bow to power of '==' operator :

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

这样,您还节省了代码处理时间,因为它不会评估每个端口号的所有条件.

This way you are also saving your code processing time as it does not evaluate all if condition for every port number.

这篇关于Django无效的代码块标记:endelse和ifequal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆