Template Inheritance
Template Inheritance
Writing redundant HTML boilerplate across dozens of views—such as repetitive <head> tags, navigation bars, CSS stylesheets, and footers—violates the Don't Repeat Yourself (DRY) principle. Django's Template Inheritance solves this using a skeleton layout architecture with overridable {% block %} sections.
1. The Master Base Layout (base.html)
Create a master layout defining the site-wide HTML skeleton and content slots:
2. Extending the Layout in Child Templates
Child templates declare which base template they inherit from using {% extends %} as the very first line:
3. Preserving Parent Content with {{ block.super }}
If you want to append content to a block rather than completely overwriting it, use {{ block.super }}:
4. Reusable Partials with {% include %}
For repeated UI fragments (like an article card or pagination bar), use {% include %}:
Practice Quiz
Function-Based Views (FBV)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Template Engine Basics | Function-Based Views (FBV) |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.