0% found this document useful (0 votes)
5 views4 pages

Temp Inheritance Session5

Uploaded by

Aniket Kadam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Temp Inheritance Session5

Uploaded by

Aniket Kadam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

by Kunal Sir

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.

Let’s look at template inheritance by starting with an example:

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

Step 2:- Create child template inside app directory in templates

A child template might look like this: # child.html

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

You might also like