Home > database >  how can I use multiple {% block content %} inside base template in django?
how can I use multiple {% block content %} inside base template in django?

Time:09-05

So, I have this base html where for example I am using it like this

<html>
<body>
<nav> navbar </nav>
<div class= "content">
{% block content %}
{% endblock %}
</div>
.............

My question is can I use multiple block contents in the same base html ? So that I can extend the parent and add elements in whatever block I want ? like this -

<html>
<body>
<nav> navbar </nav>
<div class= "content">
{% block content %}
{% endblock %}
</div>

<div class = "summaries">
{% block content %}
{% endblock %}
</div>
.............

Question might sound rookie. I am new to Django!

CodePudding user response:

You can't use two block tags with one name.

This is from docs:

Finally, note that you can’t define multiple block tags with the same name in the same template. This limitation exists because a block tag works in "both" directions. That is, a block tag doesn't just provide a hole to fill – it also defines the content that fills the hole in the parent. If there were two similarly-named block tags in a template, that template’s parent wouldn't know which one of the blocks' content to use.


Just name it something else.

  • Related