Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Template Inheritance System | Templates
Django: Build Your First Website

Template Inheritance SystemTemplate Inheritance System

In Django, template inheritance is a mechanism that allows you to create base templates and then extend or override them in derived templates. This helps to avoid code duplication and makes maintenance easier.

The basic working principle of template inheritance in Django:

Create a Base Template: Firstly, you create a base template that contains common elements shared across your entire web page. This could include a header, navigation, footer, and other elements.

AppFolde \ templates \ base.html
html

index.html

css

index.css

js

index.js

Create a Derived Template: Next, you create a derived template that extends the base template and provides specific content for its page. I'll name this template notes.

AppFolde \ templates \ notes.html
html

index.html

css

index.css

js

index.js

In this example, {% extends 'base.html' %} indicates that this template extends the base template. The {% block ... %}{% endblock %} tags define areas where content can be overridden in derived templates.

Render the Template in Django Views: In Django views, you specify which template to use for rendering a particular page.

AppFolde \ views.py

In this case, the render(request, 'notes.html') call tells Django to use the 'notes.html' template for displaying the page.

After these steps, when a visitor accesses the page, Django automatically combines the base and derived templates, inserting the content of blocks from the derived template into the corresponding blocks in the base template. This makes it easy to manage the common appearance and structure of web pages in your Django project.

Everything was clear?

Section 5. Chapter 2
course content

Course Content

Django: Build Your First Website

Template Inheritance SystemTemplate Inheritance System

In Django, template inheritance is a mechanism that allows you to create base templates and then extend or override them in derived templates. This helps to avoid code duplication and makes maintenance easier.

The basic working principle of template inheritance in Django:

Create a Base Template: Firstly, you create a base template that contains common elements shared across your entire web page. This could include a header, navigation, footer, and other elements.

AppFolde \ templates \ base.html
html

index.html

css

index.css

js

index.js

Create a Derived Template: Next, you create a derived template that extends the base template and provides specific content for its page. I'll name this template notes.

AppFolde \ templates \ notes.html
html

index.html

css

index.css

js

index.js

In this example, {% extends 'base.html' %} indicates that this template extends the base template. The {% block ... %}{% endblock %} tags define areas where content can be overridden in derived templates.

Render the Template in Django Views: In Django views, you specify which template to use for rendering a particular page.

AppFolde \ views.py

In this case, the render(request, 'notes.html') call tells Django to use the 'notes.html' template for displaying the page.

After these steps, when a visitor accesses the page, Django automatically combines the base and derived templates, inserting the content of blocks from the derived template into the corresponding blocks in the base template. This makes it easy to manage the common appearance and structure of web pages in your Django project.

Everything was clear?

Section 5. Chapter 2
some-alt