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

Template Tags की मूल बातें

Template tags special commands हैं, जैसे {% if %} और {% for %}, जो आपके HTML page को decisions लेने और खुद को repeat करने देते हैं।
Syntax
markup
{% if condition %}
  ...
{% else %}
  ...
{% endif %}

{% for item in items %}
  {{ item }}
{% endfor %}

{% if %} के साथ Conditional Rendering

{% if %} tag अपने enclosed HTML को सिर्फ तब render करता है जब condition truthy हो, एक Python if statement जैसा।

Warning: हर {% if %} को {% endif %} के साथ बंद किया जाना चाहिए।

उदाहरण: Conditional Rendering with {% if %}

Because is_premium is True, the template renders the 'Premium Member' badge; otherwise it would render 'Free Member'.

markup
<!-- context = {'is_premium': True} -->
{% if is_premium %}
  <span class="badge">Premium Member</span>
{% else %}
  <span class="badge">Free Member</span>
{% endif %}
{# 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. #}

{% for %} के साथ Looping

{% for %} tag अपने enclosed HTML को iterable में हर item के लिए एक बार repeat करता है, बिल्कुल एक Python for loop जैसा।

Note: एक {% for %} loop के अंदर, forloop.counter current 1-based iteration number देता है।

उदाहरण: Looping with {% for %}

This prints a numbered list: '1. Apple', '2. Mango', '3. Banana', using the built-in forloop.counter variable.

markup
<!-- context = {'fruits': ['Apple', 'Mango', 'Banana']} -->
<ul>
{% for fruit in fruits %}
  <li>{{ forloop.counter }}. {{ fruit }}</li>
{% endfor %}
</ul>
{# 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. #}

{% empty %} के साथ Empty Lists Handle करना

{% for %} loops एक {% empty %} clause शामिल कर सकते हैं जो सिर्फ तब render होता है जब iterable में zero items हों।

उदाहरण: Handling Empty Lists with {% empty %}

Since orders is an empty list, the {% empty %} block renders 'No orders yet.' instead of an empty <ul>.

markup
<!-- context = {'orders': []} -->
<ul>
{% for order in orders %}
  <li>{{ order.id }}</li>
{% empty %}
  <li>No orders yet.</li>
{% endfor %}
</ul>
{# 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. Closing tag भूल जाना, जैसे {% if %} बिना matching {% endif %} के लिखना, जो एक TemplateSyntaxError raise करता है।
  2. {% if %} के अंदर Python comparison operators जैसे == correctly उपयोग करना लेकिन यह भूल जाना कि and/or/not -- && / || नहीं -- valid boolean keywords हैं।
  3. {% for %} loops को nest करना बिना हर loop को एक distinct variable name दिए, confusing variable shadowing का कारण बनते हुए।
चैप्टर सारांश
  • Template tags {% tag %} syntax उपयोग करते हैं और conditionals, loops, और template composition जैसे control logic संभालते हैं।
  • {% if %}/{% elif %}/{% else %}/{% endif %} और {% for %}/{% endfor %} सबसे common tags हैं।
  • हर block tag जो खुलता है (if, for, block) का एक matching {% end... %} tag होना चाहिए।

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.