Michael Torres
Michael Torres

Reputation: 512

Django template does not exist include or extends tags

I am creating a Django project. My question is simple I believe but I am having a tough time trying to figure it out. I have an index.html in which I want to render certain things. I decided that I don't want to have a html header for every one of my html pages which to me is insane. I created a base.html file to contain a html header and I want to reuse this for my other html files but how do I do this exactly, that is if it's possible.

So what I am mostly not sure about is which tag to use {% include "base.html" %} or {% extends "base.html"%}. Intuition is telling me I should use {% include "base.html" %} because I don't believe I should be inheriting. And then my second question which relates close to this is how do I even include or extends. I just created a base.html file and an index.html file and have a index view in my views file which renders the index.html with context.

Everything works fine if I were to just use the index.html file but when I do {% include "base.html" %} in my index.html file it does not work and error says temlate base.html does not exist. Same goes if I were to use extends rather than include

Upvotes: 1

Views: 568

Answers (1)

marcanuy
marcanuy

Reputation: 23972

I don't want to have a html header for every one of my html pages

Define a block which you can define in other html pages as you need, like this:

In base.html:

    {% block header %}{% endblock %}

In index.html:

    {% block header %}
    {% include "header.html" %}
   {% endblock %}

In page_without_header.html:

    // avoid defining the header block

Upvotes: 1

Related Questions