Template के Variables
In this page:
{{ 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.
<!-- 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 करता है।
उदाहरण: 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. #}
Parentheses के बिना Methods Call करना
अगर एक dotted lookup एक callable (जैसे एक model method) पर resolve होती है, Django उसे अपने आप call करता है -- आप एक template में कभी parentheses नहीं लिखते।
उदाहरण: 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. #}
- एक template के अंदर parentheses से एक function call करने की कोशिश करना, जैसे {{ get_name() }} -- Django templates callables को parentheses के बिना अपने आप call करते हैं।
- यह मान लेना कि एक missing या misspelled variable error throw करता है -- Django इसके बजाय इसे silently empty string के रूप में render करता है।
- यह भूल जाना कि 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 करने के बजाय।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: