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
course content

Зміст курсу

Django: Build Your First Website

Django: Build Your First Website

1. Get Started
2. The First Application
3. Models
4. CRUD Operations
5. Templates
6. Forms and Dynamic URLs

bookTemplate 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.

html

index

css

index

js

index

copy

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.

html

index

css

index

js

index

copy

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.

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.

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 2
some-alt