Contenido del Curso
Django: Build Your First Website
Django: Build Your First Website
Template 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.
index
index
index
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
.
index
index
index
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.
¡Gracias por tus comentarios!