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

Template के Variables

Template variables आपके HTML में छोटी windows हैं जहां Django असली values drop करता है, जैसे एक name या एक price।
Syntax
markup
{{ variable }}
{{ object.attribute }}
{{ dictionary.key }}
{{ list.0 }}

Basic Variable Output

View के context dictionary की कोई भी key double curly braces उपयोग करके सीधे print की जा सकती है।

उदाहरण: 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. #}

Attributes और Keys के लिए Dot Notation

Dot (.) object attributes (user.username), dictionary keys (order.total), और list indices (items.0) के लिए काम करता है -- Django हर lookup type को order में try करता है।

Note: Objects और dicts के लिए एक जैसा dot notation उपयोग करना ही templates को इतना simple बनाता है।

उदाहरण: 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. #}

Parentheses के बिना Methods Call करना

अगर एक dotted lookup एक callable (जैसे एक model method) पर resolve होती है, Django उसे अपने आप call करता है -- आप एक template में कभी parentheses नहीं लिखते।

Warning: सिर्फ वे methods call करें जो कोई arguments नहीं लेते; templates एक method call को arguments pass नहीं कर सकते।

उदाहरण: 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. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक template के अंदर parentheses से एक function call करने की कोशिश करना, जैसे {{ get_name() }} -- Django templates callables को parentheses के बिना अपने आप call करते हैं।
  2. यह मान लेना कि एक missing या misspelled variable error throw करता है -- Django इसके बजाय इसे silently empty string के रूप में render करता है।
  3. यह भूल जाना कि dot notation dictionary keys और attribute/method access दोनों के लिए उपयोग होता है, जैसे {{ user.username }} objects और dicts दोनों के लिए काम करता है।
चैप्टर सारांश
  • Template variables {{ variable }} syntax उपयोग करते हैं और attributes, dict keys, और list indices access करने के लिए dot notation support करते हैं।
  • Django हर dot के लिए attribute lookup, फिर dictionary lookup, फिर list-index lookup, फिर method calls try करता है -- इस order में।
  • एक missing variable या attribute default रूप से एक empty string के रूप में render होता है error raise करने के बजाय।

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.