Solved: extends two base.html

When dealing with web development, one of the significant elements that most developers come across is presenting the same look and feel across the website. One of the most efficient ways to achieve such consistency is by using base templates or base.html in Django, a high-level Python web framework that promotes rapid development. Drupal utilizes the concept of inheritance at its best and provides a mechanism to extend base templates to multiple child templates.

To solve the problem of having a consistent design element across your website, extension of two base templates can come handy. The base templates contain the HTML skeletal structure that remains constant across the web application. The child templates contain the content for individual web pages that varies from page to page. These child templates inherit properties from the parent base templates using template inheritance.

Now let’s delve into the step-by-step explanation of the code related to the problem.

The sample base template can be defined as:

<!DOCTYPE html>
<html lang="en">
    {% block head %}
    <title>My Site</title>
    {% endblock %}
    {% block content %}
    {% endblock %}

The base.html can then be extended by a child template as:

{% extends "base.html" %}

{% block head %}
    <title>My Page</title>
{% endblock %}

{% block content %}
    <p>Here is your content...</p>
{% endblock %}

That’s about extending a single base template. But what if you have two base templates to be extended? Things get a bit tricky here as Python does not directly support multiple inheritance.

Workarounds for multiple base templates

The trick lies in dividing your content into multiple sections or blocks. Below is an example of how you can manage to extend two base templates using blocks.

{% block content %}
    {% block content_one %}
    {% endblock %}

    {% block content_two %}
    {% endblock %}
{% endblock %}

Here, the ‘content’ block from the base template is further divided into two other blocks – ‘content_one’ and ‘content_two’. Different base templates can extend these blocks.

Python Libray: Django

When it comes to web development with Python, Django is the most popular and powerful Python Web Framework. It follows DRY (Don’t Repeat Yourself) principle which encourages reusability of components. Django templates engine, part of Django framework, is basic yet powerful tool for front-end web development that allows us to extend the value of Python onto the web.

In conclusion, it is possible to extend two base templates in Django, although the framework does not directly support multiple inheritance. The key is to segment the content into multiple blocks that different base templates can extend. This is the most effective workaround to the problem and brings the power of reusability to your templates, thereby making your code more efficient and manageable.

Note: Django templating engine uses a language named Django Template Language (DTL) that borrows some of its syntax from Python but it’s itself not Python code.

Related posts:

Leave a Comment