One of the most powerful features of Django is its ability to reuse code dynamically.

Template inheritance allows sharing of code between parent and child templates.

It greatly reduces duplication of effort.

An illustration of a programmer coding on a laptop, superimposed on CSS in the background

Django has its template language designed to blend with HTML.

You will find it easy to work with Django’s templates if you’ve worked with HTML code before.

Other text-based template languages like Smarty or Jinja2 have similar syntax.

index.html  with navbar from base.html

Let’s learn more about template inheritance by building a Django project.

What’s a Django Template?

Django template tagscontrol the logic enclosing variables and values in the template.

The tags help to separate program logic from template presentation.

They also help to keep your templates clean and organized.

Tags are helpful in many ways.

They can create text in the output, perform loops, and load information into the template.

You will be using tags in this project to demonstrate template inheritance.

Create a Django Project

To get started,create a Django project.

Create an app namedtemplates.

Create a View Function

First, create aviewfunction that renders the templates.

In this case, you will render theindex.htmltemplate.

Import therendermethod from Django shortcuts.

Then create a view function named index that returns and renders the index template.

Create a URL Path

Next, create aURL pathfor the view function to display the templates.

Import thepathfunction from django.urls and theviewfunction fromviews.pyfile.

Then importsettingsandstaticto render any images and media you may have in the templates.

Create Templates

Now that you have theviewandURLpath, create the templates.

To demonstrate template inheritance, create abase.htmlas a parent template.

Thebase.htmlfile will have general elements you wish to share withindex.html,the child template.

First, load the Bootstrap and static dependencies onto thebase.htmltemplate.

You canuse the Bootstrap framework with your Django projectto style the HTML pages.

The static dependency loaded at the top will load assets included in the static folder.

Django templates allow you to pass bits of logic responsible for displaying content.

Theif/elsestatement checks for conditions in the view function.

Thebase.htmlfile will also render the contents ofnavbar.htmlright where you place the template tags.

That means whenever you extend thebase.html,navbar.htmlis also inherited.

Template variables are dynamic data provided by view functions.

Block tags allow the child templates to override the contents of the parent templates.

In this case, theindex.htmlcan replace its contents in the area enclosed by block tags.

It will not interfere with the otherbase.htmlcomponents.

In theindex.htmltemplate, you have an H1 and a paragraph element.

you might use the template tags inside the divs to call variables from theviewfunction.

Once you’ve done so, check in the online window to see whether theindex.htmlfile inherited the elements ofbase.html.

That includes Bootstrap links and thenavbar.htmltemplate.

Theindex.htmlfile should inherit the navbar and Bootstrap styles from the base template.

If so, you have used template inheritance correctly.

Without it, you would have had to add the navigation bar and Bootstrap links where you needed them.

Also, any changes you make to thebase.htmlwill reflect across all the templates it extends to.

This principle is important in error handling.

you’re able to easily Identify templates with bugs.

Template inheritance is one of the many ways Django implements the Don’t Repeat Yourself (DRY) principle.

It will make your development much easier and simpler.

Template inheritance allows you to share code between parent and child templates.

This ensures you don’t write repetitive code in your templates.

Template inheritance is important in large Django projects.

In such cases, there are many applications and many templates to design.

The parent templates give you lots of control over the program’s other components.

By learning the Django template system, you’re free to enjoy writing clean and powerful code.