2017-10-31 2 views
2

Zum Beispiel:Django Vorlagen Vererbung

base.html

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

base_index.html

{% extends 'base.html' %} 
{% block content %} 
    something 
{% endblock %} 

# add new block "for_child" to fill in with next inheritance 
<h1>Name: 
{% block for_child %} 
{% endblock %}</h1> 

base_index_child.html

{% extends 'base_index.html' %} 

{% block for_child %} 
    Peter 
{% endblock %} 

Ergebnis base_index_child.html:

<body> 
    something 
</body> 

Aber ich will (base.html -> base_index.html -> base_index_child.html)

<body> 
    something 
    <h1>Name: Peter</h1> 
</body> 

Wie dies umgehen?

Update (Antwort)

einen Block Hinzufügen muss

base_index.html

{% extends 'base.html' %} 
{% block content %} 

    something 

    <h1>Name: 
    {% block for_child %} # block must be inside the block 
    {% endblock %}</h1> 

{% endblock %} 

Antwort

1

This post ist so ziemlich in den Block sein, was Sie fragen.

So würde dies das Problem beheben:

base_index.html

{% extends 'base.html' %} 
{% block content %} 
    something 

    <h1>Name: 
    {% block for_child %} 
    {% endblock %} 
    </h1> 

{% endblock %} 
+0

Dank! Das ist was ich brauche. –