Chapter 3: Django if Tag

Django if Tag: The {% if %} tag family ({% if %}, {% elif %}, {% else %}, {% endif %})

Many beginners treat it like “just Python if”, but it’s actually a very limited, very safe mini-language with its own rules, shortcuts, and common pitfalls.

Today we’re going to learn it properly — like I’m sitting next to you, writing in VS Code together, explaining every single pattern you’ll actually use in real projects (not just theory).

We’ll use your existing polls app (Question + Choice + admin + templates) as the playground.

1. The Absolute Basic Form (Everyone Starts Here)

HTML

→ If question.is_active is True → show the message → If False (or None, empty string, empty list, zero) → show nothing

2. With {% else %} (Most Common Pattern)

HTML

3. Full {% if %} … {% elif %} … {% else %} Chain

HTML

Very important rule: Django evaluates conditions left to right — first True block wins.

4. Testing for “Falsy” Values (Django’s Special Truthy/Falsy)

Django’s if considers these False (same as Python):

  • False
  • None
  • 0 (integer)
  • 0.0 (float)
  • empty string “”
  • empty list []
  • empty dict {}
  • empty queryset (.exists() is False)

Everything else is True.

Examples:

HTML

5. Using Operators (Comparisons)

Django supports these inside {% if %}:

  • == / !=
  • < / <= / > / >=
  • in / not in
  • is / is not (mostly for None)

Examples:

HTML

Very common real-life pattern:

HTML

6. Combining with and, or, not

HTML

Tip: Use parentheses for clarity when mixing and/or

HTML

7. Checking for Existence / Emptiness (Very Frequent)

HTML

8. Testing Permissions (Very Common in Real Projects)

HTML

9. Quick Real-World Examples from Your Polls App

Put these in your index.html or detail.html

HTML

Common Beginner Traps & Fixes

Problem What happens Fix / Correct syntax
{% if question.vote_count > 5 %} works but == fails Comparison operators are allowed, but spaces matter Always put spaces around operators: >, ==, !=
{% if question.vote_count %} shows even when 0 0 is falsy Use {% if question.vote_count != 0 %} or {% if question.vote_count > 0 %}
{% if question.category == fun %} fails No quotes {% if question.category == “fun” %}
{% if not question %} shows wrong thing question is object Use {% if not question %} for None / missing
Too many nested ifs → messy code Move logic to view/context or custom template tag

Your Quick Practice Task (Do This Right Now)

Open polls/templates/polls/detail.html or index.html and try adding these:

  1. {% if question.vote_count > 10 %}Popular!{% endif %}
  2. {% if question.is_active and question.vote_count > 0 %}Active & voted{% else %}Not ready{% endif %}
  3. {{ question.category|default:”Uncategorized” }}
  4. {% if forloop.first %}<strong>Featured poll:</strong>{% endif %} inside a for loop
  5. {% if not user.is_authenticated %}Please login to vote{% endif %}

Tell me what feels next:

  • Which if pattern is still confusing? (comparisons? falsy values? permissions?)
  • Want to see 20 more real-life {% if %} examples from different apps?
  • Ready to learn {% ifchanged %} / {% ifequal %} (older but still used)?
  • Or want to move to custom template tags/filters?
  • Or finally ready for Django Forms + Voting + POST + F()?

You’re building very strong template logic now — this {% if %} family is used on almost every page you’ll ever write.

Keep practicing — you’re doing great! 🚀🇮🇳

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *