imsaiful
imsaiful

Reputation: 1614

<title> {% block title %} Home {% endblock %} </title> is not override by other page?

This is my base file

{% load static %}
{% include "feed/header.html" %}
{% block content%}

{% endblock %}
{% include "feed/footer.html" %}

This is my header which includes title:

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
     <title>
        {% block title %} Home {% endblock %}
     </title>

So when I try to override title in my detail page then it shows the title of header always i.e. Home(not change) but not the title of detail page which I override. following is the code which I used in Detail page:

{% extends 'feed/base.html'%}
{% block title %} Details {% endblock %}

{% block content %}

some views
{% endblock %}

So help me to figure out this problem.

Upvotes: 3

Views: 9097

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476729

As the documentation says, you can only "override" blocks defined in templates from which you extend directly or indirectly (i.e. you extend from a template that extends itself):

The include tag should be considered as an implementation of “render this subtemplate and include the HTML”, not as “parse this subtemplate and include its contents as if it were part of the parent”. This means that there is no shared state between included templates – each include is a completely independent rendering process.

Blocks are evaluated before they are included. This means that a template that includes blocks from another will contain blocks that have already been evaluated and rendered - not blocks that can be overridden by, for example, an extending template.

You thus need to inline your block into the parent template. For example:

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
     <title>
        {% block title %} Home {% endblock %}
     </title>
</head>
{% block content%}

{% endblock %}
{% include "feed/footer.html" %}

and then thus override it in the "subtemplate".

Upvotes: 5

Daniel Roseman
Daniel Roseman

Reputation: 599630

The docs for the include tag are explicit that this will not work:

Blocks are evaluated before they are included. This means that a template that includes blocks from another will contain blocks that have already been evaluated and rendered - not blocks that can be overridden by, for example, an extending template.

Upvotes: 3

Related Questions