← Back to Django Course | Chapter 3: Templates & Template Language | Lesson 3 of 10

Template Variables

Template variables are little windows in your HTML where Django drops in real values, like a name or a price.

Basic Variable Output

Any key from the view's context dictionary can be printed directly using double curly braces.

Example: Basic Variable Output

product_name and price are looked up directly from the context dict and printed as text.

markup
<!-- context = {'product_name': 'Notebook', 'price': 250} -->
<h2>{{ product_name }}</h2>
<p>Price: ₹{{ price }}</p>
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Dot Notation for Attributes and Keys

The dot (.) works for object attributes (user.username), dictionary keys (order.total), and list indices (items.0) -- Django tries each lookup type in order.

Note: Using dot notation the same way for objects and dicts is what makes templates so simple.

Example: Dot Notation for Attributes and Keys

user.username reads an object attribute; cart.items and cart.total read dictionary keys -- same dot syntax for both.

markup
<!-- context = {'user': request.user, 'cart': {'items': 3, 'total': 599}} -->
<p>Welcome, {{ user.username }}</p>
<p>Items in cart: {{ cart.items }}</p>
<p>Total: ₹{{ cart.total }}</p>
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Calling Methods Without Parentheses

If a dotted lookup resolves to a callable (like a model method), Django calls it automatically -- you never write parentheses in a template.

Warning: Only call methods that take no arguments; templates can't pass arguments to a method call.

Example: Calling Methods Without Parentheses

{{ user.get_full_name }} automatically invokes get_full_name() on the user object and prints its return value.

markup
<!-- model has: def get_full_name(self): return f"{self.first} {self.last}" -->
<p>{{ user.get_full_name }}</p>
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Trying to call a function with parentheses inside a template, e.g. {{ get_name() }} -- Django templates call callables automatically without parentheses.
  2. Assuming a missing or misspelled variable throws an error -- Django silently renders it as an empty string instead.
  3. Forgetting dot notation is used for both dictionary keys and attribute/method access, e.g. {{ user.username }} works for both objects and dicts.
Chapter Summary
  • Template variables use {{ variable }} syntax and support dot notation to access attributes, dict keys, and list indices.
  • Django tries attribute lookup, then dictionary lookup, then list-index lookup, then method calls -- in that order -- for each dot.
  • A missing variable or attribute renders as an empty string by default rather than raising an error.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.