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

{% include %} से Templates Include करना

{% include %} एक reusable sticker चिपकाने जैसा है -- एक छोटा HTML chunk, जैसे एक navbar, को जितनी चाहें उतनी pages में।
Syntax
markup
{% 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.

markup
<!-- 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.

markup
<!-- 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 करने देता है।

Note:
  • छोटे 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.

markup
<!-- 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. #}
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. {% include %} को {% extends %} के साथ confuse करना -- include एक reusable fragment paste करता है, extends एक पूरा page layout inherit करता है।
  2. यह भूल जाना कि एक included template को अपने खुद के variables explicitly with से pass करने की ज़रूरत है अगर यह parent के context में न होने वाले names पर depend करता है।
  3. 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 है।

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.