Template Variables
In this page:
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.
<!-- 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.
<!-- 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.
<!-- 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. #}
- Trying to call a function with parentheses inside a template, e.g. {{ get_name() }} -- Django templates call callables automatically without parentheses.
- Assuming a missing or misspelled variable throws an error -- Django silently renders it as an empty string instead.
- Forgetting dot notation is used for both dictionary keys and attribute/method access, e.g. {{ user.username }} works for both objects and dicts.
- 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.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: