Ad

Django Extends Only One Template

- 1 answer

I have an app called exercise_tracker and want to extend my base.html with two templates: exercise_list.html and footer.html. For exercise_list.htmlI have set a view for footer.html not. It doesn't render the content of footer.html. Can you give me a hint what I'm doing wrong?

views.py

from django.shortcuts import render
from django.utils import timezone
from .models import Exercise

def exercise_list(request):
    exercises = Exercise.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, 'exercise_tracker/exercise_list.html', {"exercises": exercises})

base.html

<html>
    <body>
        {% block content %}
        {% endblock %}

        {% block footer %}
            <p>Footer placeholder</p>
        {% endblock %}
    </body>
</html>

exercise_list.html

{% extends 'exercise_tracker/base.html' %}

{% block content %}
    <table>
    {% for exercise in exercises %}
        <tr>
            <td>{{ exercise.date_learned }}</td>
            <td>{{ exercise.time_studied }}</td>
            <td>{{ exercise.language_learned }}</td>
            <td>{{ exercise.description|linebreaksbr }}</td>
        </tr>
    {% endfor %}
    </table>
{% endblock %}

footer.html

{% extends 'exercise_tracker/base.html' %}

{% block footer %}
    <p>Footer</p>
{% endblock %}
Ad

Answer

Using the extends template tag tells Django to populate the blocks in the parent template with the block content defined in the used child template only!

When you populate only the content block in your exercice_list.html then only the content block of the base.html will be populated when you call your exercice_list view. This is because django does not look for what other templates might extend the parent template, so it doesn't care that there is a footer.html somewhere that also extends it. If you want the content from footer.html inclded when you call the exercice_list view then you need to add it either already in the base.html or then in the exercice_list.html.

It is totally fine to keep the footer in a separate template, but you need to add it, also as a block, to the exercice_list.html:

{% extends 'exercise_tracker/base.html' %}
{% block content %}
<table>
{% for exercise in exercises %}
    <tr>
        <td>{{ exercise.date_learned }}</td>
        <td>{{ exercise.time_studied }}</td>
        <td>{{ exercise.language_learned }}</td>
        <td>{{ exercise.description|linebreaksbr }}</td>
    </tr>
{% endfor %}
</table>
{% endblock %}

{% block footer %}
    {% include "footer.html" %}
{% endblock %}

And your footer.html might look like:

<p>Footer</p>

If you want to do some more reading about this, an SO question about include and extend is a good starting point: django templates: include and extends Or then the excellent django docs about template inheritance.

Hope that helped and happy coding!

Ad
source: stackoverflow.com
Ad