Chapter 1: Django Template Variables

Template variables — that is: everything that happens between {{ and }}

Most beginners think “it’s just printing a variable”… but there is a whole little language inside those double curly braces. Understanding it properly will make your templates 5× cleaner, faster to write, and much less error-prone.

Let’s go slowly — like I’m sitting next to you, typing in VS Code together, explaining every single syntax variation you’ll actually use in real projects.

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

1. The Most Basic Form (What Everyone Knows)

HTML

→ Prints the value → Automatically escapes HTML/JS (safe by default)

2. Dot Notation – Accessing Attributes & Dictionary Keys

HTML

Golden rule: Django tries in this order:

  1. Dictionary lookup → dict[“key”]
  2. Attribute / property lookup → obj.attribute
  3. Method call (without arguments) → obj.method()
  4. List-index lookup → list[0]

So {{ question.was_published_recently }} actually calls the method!

3. Filters – The Real Power Inside {{ }}

Filters are small functions applied with

HTML

Most used filters in real projects (keep this list handy)

  • date:”FORMAT”
  • time:”FORMAT”
  • timesince / timeuntil
  • truncatewords:N / truncatechars:N
  • default:”value”
  • pluralize:”s,es” or pluralize:”vote,votes”
  • add:N / sub:N
  • length / length_is:”5″
  • lower / upper / capfirst / title
  • safe (dangerous – only if you trust the content)
  • linebreaksbr (turns \n into )
  • striptags (removes HTML tags)
  • floatformat:”-2″ (2 decimal places)

4. Chaining Filters (Very Common)

HTML

Order matters — filters run left to right.

5. Variables in Loops & Conditions

HTML

Special loop variables:

  • {{ forloop.counter }} → 1,2,3…
  • {{ forloop.counter0 }} → 0,1,2…
  • {{ forloop.first }} / {{ forloop.last }} → boolean
  • {{ forloop.revcounter }} → reverse count

6. Accessing Context Variables Safely

HTML

7. Custom Filters & Tags (When Built-ins Are Not Enough)

Create polls/templatetags/poll_extras.py:

Python

In template:

HTML

Quick Syntax Cheat-Sheet (Print & Keep)

text

Your Quick Task Right Now

Open one of your templates (index.html or detail.html) and try these:

  1. {{ question.pub_date|date:”d F Y, l” }} → day name too
  2. {{ question.vote_count|pluralize:”vote,votes” }}
  3. {{ question.question_text|truncatewords:12|capfirst }}
  4. Add {{ forloop.counter }}. before each question in the list
  5. Try {{ question.category|default:”Uncategorized” }}

Tell me what feels next:

  • Which part of template variables is still confusing? (filters? chaining? custom filters?)
  • Want to see more filter examples (date, numbers, strings)?
  • Want to practice custom filters/tags together?
  • Or ready for the next big topic: Forms + Voting + POST + F() expression

You’re getting really strong with Django syntax — this is the foundation everything else builds on.

Keep asking, keep typing — you’re doing great! 🚀🇮🇳

You may also like...

Leave a Reply

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