How to check if, elif, else conditions at the same time in django template
<p>I have 3 category in category field. I want to check it in django template and assign appropirte urls for 3 distinct category.</p>

<p>I tried:</p>

<pre><code>{% for entry in entries %}
        {% if entry.category == 1 %}
        &lt;a href="{% url 'member:person-list' %}"&gt;&lt;li&gt;{{ entry.category }}&lt;/li&gt;&lt;/a&gt;
        {% elif entry.category == 2 %}          
        &lt;a href="{% url 'member:execomember-list' %}"&gt;&lt;li&gt;{{ entry.category}}&lt;/li&gt;&lt;/a&gt;
         {% else %}
         &lt;a href="{% url 'member:lifemember-list' %}"&gt;&lt;li&gt;{{ entry.category}}&lt;/li&gt;&lt;/a&gt;
        {% endif %}
{% empty %}
    &lt;li&gt;No recent entries&lt;/li&gt;
{% endfor %}
</code></pre>

<p>But I know python only check first matching condition with if. Therefore it gave only one desired result. How do I get all three entries with their correct links?</p>

<p><strong>Edit:</strong></p>

<p>Though python only check first matching if condition, when use elif within for loop it check each condition until endfor loop. Therefore my answer below worked fine. </p>

<p>From what I understand, you want to associate each entry to a url depending on the category it belongs among the 3 categories. you can have this refactored in your <code>Entry</code> model in order to minimize logic in templates like:</p>

<pre><code>class Entry(models.Model):
    category = ...

    def get_entry_url(self):
        if self.category == 'General Member':
            return reverse for the url 'member:person-list'

        elif self.category == 'Executive Committee Member':
            return reverse for the url 'member:execomember-list'

        else:
            return reverse for the url 'member:person-list'
</code></pre>

<p>Then in template:</p>

<pre><code>{% for entry in entries %}
     &lt;a href="{{ entry.get_entry_url }}"&gt;&lt;li&gt;{{ entry.category }}&lt;/li&gt;&lt;/a&gt;
{% empty %}
    &lt;li&gt;No recent entries&lt;/li&gt;
{% endfor %}
</code></pre>

python django