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

extends से Template Inheritance

Template inheritance एक photo frame जैसा है: एक base template shared layout रखता है, और हर page सिर्फ अपनी picture swap करता है।
Syntax
markup
<!-- base.html -->
{% block block_name %}default content{% endblock %}

<!-- child.html -->
{% extends 'base.html' %}
{% block block_name %}
  child content
{% endblock %}

एक Base Template Define करना

एक base template overall page structure define करता है (जैसे <html>, navigation, footer) और replaceable regions को {% block %} tags से mark करता है।

उदाहरण: Defining a Base Template

This base.html defines two blocks -- title and content -- that child templates can fill in or override.

markup
<!-- templates/base.html -->
<!DOCTYPE html>
<html>
<head><title>{% block title %}My Site{% endblock %}</title></head>
<body>
  <nav>Home | About</nav>
  {% block content %}{% endblock %}
</body>
</html>
{# 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. #}

Base Template को Extend करना

एक child template {% extends %} से शुरू होता है और सिर्फ उन blocks को redefine करने की ज़रूरत है जिन्हें वह customize करना चाहता है।

Note: {% extends %} file में सबसे पहला tag होना चाहिए, किसी भी दूसरे content से पहले।

उदाहरण: Extending the Base Template

home.html reuses base.html's full layout, replacing only the title and content blocks.

markup
<!-- templates/home.html -->
{% extends "base.html" %}

{% block title %}Home{% endblock %}

{% block content %}
  <h1>Welcome to the homepage!</h1>
{% 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. #}

block.super के साथ Parent Content रखना

एक child block के अंदर {{ block.super }} call करना parent block के original content को पूरी तरह replace करने के बजाय insert करता है।

उदाहरण: Keeping Parent Content with block.super

This keeps whatever the parent's content block defined and appends the extra paragraph after it.

markup
{% block content %}
  {{ block.super }}
  <p>Extra info shown only on this page.</p>
{% 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. यह भूल जाना कि {% extends %} को एक child template में बिल्कुल पहली line होना चाहिए -- इससे पहले कुछ भी होने पर एक error होता है।
  2. Child template में एक {% block %} को ऐसा नाम देना जो parent में defined किसी block name से match नहीं करता, इसलिए content silently drop हो जाता है।
  3. {{ block.super }} call न करना जब वे असल में parent block का content रखना और उसमें add करना चाहते थे, पूरी तरह replace करने के बजाय।
चैप्टर सारांश
  • {% extends 'base.html' %} एक child template को एक parent template का layout inherit करने देता है।
  • {% block name %}...{% endblock %} parent में एक section define करता है जिसे children override कर सकते हैं।
  • एक child template को सिर्फ उन blocks को define करने की ज़रूरत है जिन्हें वह बदलना चाहता है -- बाकी सब parent पर fall back होता है।

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.