changzhi
changzhi

Reputation: 2709

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

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

Upvotes: 2

Views: 2523

Answers (3)

hemraj
hemraj

Reputation: 1034

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.

Upvotes: 6

Raphael Laurent
Raphael Laurent

Reputation: 1951

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

Upvotes: 2

sundar nataraj
sundar nataraj

Reputation: 8702

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

Upvotes: 0

Related Questions