Django template rendering order... - problem with include and partial and blocks
I've got:
ViewChild.html:
{% extends 'app1/parent.html' %}
{%block title%}
my title
{%endblock title%}
Then I've got Parent.html:
{%include 'html_parts/modal_part.html' %}
That uses partial view:
{%block title%}
{%endblock title%}
Unfortunately, the view is not rendered correctly ie. 'my title' is not displayed. (it is all ok with application, paths, etc. All files are placed in coorect folders). It is only something with the logic of rendering. Possibly, I can't use in parent view a partial, that defines block, that is going to be filled by child view? Any advices?
The reason why "my title" is not being displayed is because of how include
works. From the documentation: include
"loads a template and renders it with the current context."
This is different from how block
works in template inheritance: block tells "the template engine that a child template may override those portions of the template." (Documentation)
What this means in your case is that you would need to update Parent.html
to remove {% include 'html_parts/modal_part.html' %}
and add {%block title %}
, which allows ViewChild.html to override the value of
title`.