elif in django template

 {% for entry in entries %}
        {% if entry.category == 'General Member' %}
        
  • {{ entry.category }}
  • {% elif entry.category == 'Executive Committee Member' %}
  • {{ entry.category}}
  • {% else %}
  • {{ entry.category}}
  • {% endif %} {% empty %}
  • No recent entries
  • {% endfor %}

    Here is what the above code is Doing:
    1. It’s looping through the entries in the database.
    2. It’s checking if the entry is a General Member.
    3. If it is, it’s displaying a link to the person-list page.
    4. If it’s not, it’s checking if the entry is an Executive Committee Member.
    5. If it is, it’s displaying a link to the execomember-list page.
    6. If it’s not, it’s displaying a link to the person-list page.
    7. If there are no entries in the database, it’s displaying a message saying so.

    The above code will work fine if there are only two types of members in the database. But what if there are more?

    The code will become more and more complicated as the number of member types increases.

    A better way to handle this would be to use a dictionary.

    Here’s how you could do that: