Temp Inheritance Session5
Temp Inheritance Session5
Template inheritance
Template inheritance allows you to build a base “skeleton” template that contains all the
common elements of your site and defines blocks that child templates can override.
Step 1:- Create new html file inside templates directory by the name base / layout.html.
Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk Road,
Karve Nagar, Pune, Maharashtra 411052 , Mobile No.- 8888022204
by Kunal Sir
# base.html
It’s the job of “child” templates to fill the empty blocks with content.
In this example, the block tag defines three blocks that child templates can fill in. All the block
tag does is to tell the template engine that a child template may override those portions of the
template.
Note :
We can override only those blocks which are defined in parent template.
It is not necessary to override all blocks of parent
If you use {% extends “ parent.html file ” %} in a template, it must be the first
template tag in that template. Template inheritance won’t work, otherwise.
Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk Road,
Karve Nagar, Pune, Maharashtra 411052 , Mobile No.- 8888022204
by Kunal Sir
The extends tag is the key here. It tells the template engine that this template “extends” another
template. When the template system evaluates this template, first it locates the parent – in this
case, “base.html”.
At that point, the template engine will notice the three block tags in base.html and replace those
blocks with the contents of the child template. Depending on the value the output might look
like:
Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk Road,
Karve Nagar, Pune, Maharashtra 411052 , Mobile No.- 8888022204
by Kunal Sir
# child.html
Note that since the child template didn’t define the first div block, the value from the parent
template is used instead. Content within a {% block %} tag in a parent template is always used
as a fallback
Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk Road,
Karve Nagar, Pune, Maharashtra 411052 , Mobile No.- 8888022204