Django supports template inheritance. The concept of inheritance in Django is very similar to inheritance in object-oriented programming. The idea is that the output rendered by each view in a web application must follow a uniform pattern or look, even though each view may render a different template.
Suppose a Django app has three URL routes registered with three views. We want to design the template in such a way that each view should have a page header, a footer and a sidebar with links and the variable content displayed to its right.
The Master Template
A base class in any object-oriented language (such as Python) defines attributes and methods and makes them available to the inherited class. In the same way, we need to design a master template that provides an overall skeleton for other templates.
The master template (sometimes called “base template”), along with the common structure, also marks the dummy blocks. The child template inherits the common structure and overrides the blocks to provide respective contents. Such blocks are marked with “block – endblock” construct.
{% block block_name %}......{% endblock %}
The master template may have more than one such blocks in different places. Each one should be provided a unique identifier.
The HTML code for our master template (base.html) is as follows −
<h2 align="center">This is Home page</h2>
{% endblock %}
</body></html>
Define a View
Let us define a view that renders this template −
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.defindex(request):return render(request,"index.html",{})