Can Django template extend dynamic content in child or grandchild template?

77 views
Skip to first unread message

tristant

unread,
Oct 14, 2020, 7:03:43 PM10/14/20
to Django users

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

category.html :

{% extends "base.html" %} {% block content %} <div class="category_row"> <div id="menu_category_display"> {% for category in menu_categories %} {% with category_button="menu_"|add:category.name|lower %} <button class="button category_buttons {{category_button}}" onclick="showItem('{{category_button}}','category_buttons')"> {{category}} </button> {% endwith %} {% endfor %} </div> </div> <div class="item_row"> {% block level_2_content %} {% endblock %} </div> </div> {% endblock %}

and item.htlm

{% extends "category/category.html" %} {% block level_2_content %} <div id="test_div"> {% for item in menu_items %} <p>{{item.name}}</p> {% endfor %} </div> {% endblock %}

When item.html is rendered, all the elements that were rendered by category.html are gone. Only the elements of the base.html are retained.

In the code above, the entire category_row div does not appear in item.html. How do I retain them?

How do I retain all elements in all parent templates instead of just base.html? A general method to do this for more than 3 levels of extension like in my case?


tristant

unread,
Oct 14, 2020, 7:04:55 PM10/14/20
to Django users

Thanks,
Reply all
Reply to author
Forward
0 new messages