Jorge
Jorge

Reputation: 37

Django ignores content in child template if I use {% extends %}

I am using a parent template called "base.html" placed in the directory myproject/myproject/templates. I render the child template, and everything goes well except when I write {% extends %} to show the parent template. Django behaves very strangely. After a long time trying to figure out the mistake, I've discovered that, even if I write the extends, block, endblock stuff related to the parent template inside a comment, Django still ignores the content in the child template and shows an empty screen. If I intentionally make a syntax error in the content of the template, Django throws an exception (so it really reads the content), but if there is no error, the browser only takes the code from the parent template (I've discovered this using Inspect). Django only shows the content in the child template if it stops having a relationship with the parent template (I can't even have the code extends, block, etc. inside a comment)

Here's the code:

child template:

{% extends "education/base.html" %}
{% block barraup %}
{{block.super}}
{%endblock%}

{% block barradown %}
{{block.super}}
{%endblock%}

{% load static %}
<link rel="stylesheet" type="text/css" href = "{% static 'principal/principal.css' %}" />


<div id="presentacion">
    ...content
</div>

The last div "presentacion" won't even be read by the browser if there are no errors (it won't appear in the html that appears in the Inspect screen).

parent template:

{% load static %}
<head>
    <link rel="stylesheet" type="text/css" href = "{% static 'education/base.css' %}">
</head>

<body>
        <div id="barraup">
        {% block barraup %}
        ...content
        {% endblock %}
        </div>

        <div id="barradown">
        {% block barradown %}
        ...content
        {% endblock %}
        </div>

</body>
</html>

Thank you in advance for your help.

Upvotes: 1

Views: 518

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599480

Everything in a child template needs to be within a block that is defined in the parent - otherwise there is nowhere to put it.

You should probably define a "content" block between those two divs in the parent, and wrap the child content in that.

Upvotes: 4

Related Questions