Django Extends Only One Template
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.html
I 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 %}
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!
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module