Django templates - include and repeat the block contents

965 Views Asked by At

In my home.html page, I am trying to include a header.html file along with extending base.html. Following is my code

{% extends "base.html" %}

{% block body %}
   {% include 'header.html' %}

   # including the block navigation from header.html
   <nav id='header-nav'>{% block nav %} {% endblock %}</nav>

   # including the block image from header.html
   <div id='header-img'>{% block image %} {% endblock %}</div>

   # Reusing the same navigation in footer from header.html
   <div id='footer-nav'>{% block nav %} {% endblock %}</div>

{% endblock %}

Home.html looks like the following

{% block image %}<h1>I am image</h1>{% endblock %}
{% block nav %}<h1>I am navigation</h1>{% endblock %}

However, it returns an error - ''block' tag with name 'nav' appears more than once'.

Why is that? Is there any solutions to this?

Regards

1

There are 1 best solutions below

2
On

You included {% block nav %} twice in the same template. That's why it's throwing the error. Maybe you meant to do {% block footer %}?

{% extends "base.html" %}

{% block body %}
   {% include 'header.html' %}

   # including the block navigation from header.html
   <nav id='header-nav'>{% block nav %} {% endblock %}</nav>

   # including the block image from header.html
   <div id='header-img'>{% block image %} {% endblock %}</div>

   # Name this block something else i.e add a new block in header.html
   # and this error should clear up.
   <div id='footer-nav'>{% block footer %} {% endblock %}</div>

{% endblock %}