2

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 %}
j-i-l
  • 10,281
  • 3
  • 53
  • 70
didierCH
  • 378
  • 2
  • 17
  • 2
    I don't think two extends work together. You can use extend for your main content and add header and/or footer with include tag. – Bidhan Majhi Dec 22 '18 at 09:17

3 Answers3

2

I suggest you edit your 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 %}
{% block footer %}
   <p>Footer placeholder</p>
{% endblock %}

In base.html

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

        {% block footer %}
        {% endblock %}
    </body>
</html>

It may solve your problem.

shafik
  • 6,098
  • 5
  • 32
  • 50
2

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!

j-i-l
  • 10,281
  • 3
  • 53
  • 70
0

You dont need a separate template for footer. If you are using same footer for the entire website you can just code it in base.html . If you want to override it only someplaces just add a block for footer in the base template and ovverride it where you want to.

This should give you an idea :

Base.html :

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
    // Site-Wide Links , CDN go here
    {% block head %} 
    {% endblock %
  </head>
  <body>
// Base code goes here
    {% block content %}
    {% endblock %}    
<footer>
// Base Footer goes here
    {% block footer %}
    {% endblock %}

</footer>

  </body>
</html>

Some_template.html :

{% extends 'exercise_tracker/base.html' %} 
    {% block head %}
    //Your CSS , CDN here 
    {% endblock %}

    {% block content %}
 // Your HTML here
    {% endblock %}

    {% block footer %}
//Your Footer Here if you need to add something
    {% endblock %}
DarkSied
  • 49
  • 1
  • 6