extends से Template Inheritance
In this page:
<!-- 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.
<!-- 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 करना चाहता है।
उदाहरण: Extending the Base Template
home.html reuses base.html's full layout, replacing only the title and content blocks.
<!-- 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.
{% 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. #}
- यह भूल जाना कि {% extends %} को एक child template में बिल्कुल पहली line होना चाहिए -- इससे पहले कुछ भी होने पर एक error होता है।
- Child template में एक {% block %} को ऐसा नाम देना जो parent में defined किसी block name से match नहीं करता, इसलिए content silently drop हो जाता है।
- {{ 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 होता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: