{% include %} से Templates Include करना
In this page:
{% include 'partial_name.html' %}
{% include 'partial_name.html' with key=value %}
एक Reusable Fragment Include करना
{% include %} एक अलग template file render करता है और इसका output current template में उस spot पर directly drop कर देता है।
उदाहरण: Including a Reusable Fragment
When home.html renders, Django inserts navbar.html's rendered HTML right where {% include %} appears.
<!-- templates/navbar.html -->
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>
<!-- templates/home.html -->
{% include "navbar.html" %}
<h1>Welcome!</h1>
{# 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. #}
'with' से Extra Context Pass करना
with clause आपको parent का context बदले बिना included template में extra variables pass करने देता है।
उदाहरण: Passing Extra Context with 'with'
label is only available inside card.html for this specific include -- it doesn't leak into the rest of the parent template.
<!-- templates/card.html expects {{ label }} -->
{% include "card.html" with label="Featured Product" %}
{# 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. #}
include vs extends
include page में एक spot पर एक reusable fragment paste करता है; extends एक पूरा base layout inherit करता है और आपको इसके अंदर named blocks override करने देता है।
- छोटे repeated pieces (navbar, card, footer) के लिए include उपयोग करें
- Whole-page structure के लिए extends उपयोग करें।
उदाहरण: include vs extends
Both tags reuse templates, but include is for fragments while extends is for full-page inheritance.
<!-- fragment reused on many pages -->
{% include "footer.html" %}
<!-- vs inheriting a full page layout -->
{% extends "base.html" %}
{% block content %}...{% endblock %}
{# 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. #}
- {% include %} को {% extends %} के साथ confuse करना -- include एक reusable fragment paste करता है, extends एक पूरा page layout inherit करता है।
- यह भूल जाना कि एक included template को अपने खुद के variables explicitly with से pass करने की ज़रूरत है अगर यह parent के context में न होने वाले names पर depend करता है।
- Actually page-specific content के लिए include को overuse करना, जब एक template block ज़्यादा clear होता।
- {% include 'template.html' %} उस point पर एक दूसरे template का rendered output insert करता है।
- Included templates default रूप से parent का context share करते हैं, या आप with उपयोग करके extra variables pass कर सकते हैं।
- कई pages में उपयोग होने वाले reusable fragments जैसे navbars, footers, और cards के लिए include ideal है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: