Django / Django Templates

Creating and Extending Base Templates

This tutorial delves into the concept of Template Inheritance in Django. We will learn how to create a base template and extend it in child templates, promoting code reuse and con…

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Covers the Django templating engine, template inheritance, and dynamic content rendering.

1. Introduction

1.1. Tutorial's Goal

This tutorial aims to provide a comprehensive understanding of template inheritance in Django. By the end of this tutorial, you will learn how to create a base template and extend it in child templates.

1.2. What the user will learn

  • Basics of Template Inheritance
  • Creating a Base Template
  • Extending a Base Template in Child Templates

1.3. Prerequisites

A basic understanding of Django, Python, and HTML is required. Familiarity with Django's templating language will be helpful.

2. Step-by-Step Guide

2.1. What is Template Inheritance?

Template inheritance is a feature in Django's templating system that allows you to build a base "skeleton" template that contains all the common elements of your site, and then extend it in child templates for different parts of your site.

2.2. Creating a Base Template

A base template usually contains elements like the site title, navigation bar, footer etc. Here's an example:

<!-- base.html -->
<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}{% endblock %}</title>
</head>
<body>
    <header>
        <!-- navigation bar -->
    </header>
    <main>
        {% block content %}
        {% endblock %}
    </main>
    <footer>
        <!-- footer -->
    </footer>
</body>
</html>

In this example, {% block title %}{% endblock %} and {% block content %}{% endblock %} are placeholders for content that will be filled in by child templates.

2.3. Extending a Base Template

To extend a base template, we use the {% extends "base.html" %} tag at the top of the child template. Here's an example:

<!-- home.html -->
{% extends "base.html" %}

{% block title %}
Home Page
{% endblock %}

{% block content %}
<p>Welcome to the Home Page!</p>
{% endblock %}

In this example, the {% block title %} and {% block content %} tags in the base template are replaced with the content in the child template.

3. Code Examples

Here are some more practical examples:

3.1. Example 1: Extending the Base Template with Different Titles

<!-- about.html -->
{% extends "base.html" %}

{% block title %}
About Page
{% endblock %}

{% block content %}
<p>Welcome to the About Page!</p>
{% endblock %}

3.2. Example 2: Extending the Base Template with Multiple Content Blocks

<!-- base.html -->
<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}{% endblock %}</title>
</head>
<body>
    <header>
        <!-- navigation bar -->
    </header>
    <main>
        {% block content1 %}
        {% endblock %}
        {% block content2 %}
        {% endblock %}
    </main>
    <footer>
        <!-- footer -->
    </footer>
</body>
</html>

<!-- home.html -->
{% extends "base.html" %}

{% block title %}
Home Page
{% endblock %}

{% block content1 %}
<p>Welcome to the Home Page!</p>
{% endblock %}

{% block content2 %}
<p>Enjoy your stay.</p>
{% endblock %}

4. Summary

In this tutorial, we've learned about template inheritance in Django, how to create a base template, and extend it in child templates. This promotes code reuse and consistency across your site.

5. Practice Exercises

5.1. Exercise 1

Create a base template with a navigation bar, main content area, and footer. Then create three child templates (Home, About, Contact) that extend the base template.

5.2. Exercise 2

Modify the base template from Exercise 1 to include a sidebar content block. Extend this new base template in a child template.

5.3. Exercise 3

Create a base template with two content blocks. Then create a child template that only fills one of the content blocks.

Solutions

Solution to Exercise 1

Link to Solution

Solution to Exercise 2

Link to Solution

Solution to Exercise 3

Link to Solution

For further practice, try extending these exercises by adding more content blocks, more child templates, or more complex HTML structures.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Favicon Generator

Create favicons from images.

Use tool

Watermark Generator

Add watermarks to images easily.

Use tool

HTML Minifier & Formatter

Minify or beautify HTML code.

Use tool

Meta Tag Analyzer

Analyze and generate meta tags for SEO.

Use tool

EXIF Data Viewer/Remover

View and remove metadata from image files.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help