Skip to content Skip to sidebar Skip to footer

Django Invalid Block Tag: Endelse And Ifequal

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

Solution 1:

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.


Solution 2:

The equality operator is supported in Django. To solve your code issue, I'd have used :

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

Solution 3:

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

for every ifequal u need to close it with endifequal u missed one


Post a Comment for "Django Invalid Block Tag: Endelse And Ifequal"