"<ExtendsNode: extends "base.html"> must be the first tag in the template." error in Django Templates
Asked Answered
S

6

5

I'm using:

{% extends "base.html" %}

I get the following error:

<ExtendsNode: extends "base.html"> must be the first tag in the template.

Can you please help me?

Shayshaya answered 10/8, 2010 at 18:57 Comment(0)
C
6

It must be the very first django template tag in your template.

Documentation says:

If you use {% extends %} in a template, it must be the first template tag in that template. Template inheritance won't work, otherwise.

Documentation can be found here

Cacogenics answered 10/8, 2010 at 19:18 Comment(0)
S
6

place {% extends "base.html" %} in line 1 of your editor. Literally put it in line 1. REMOVE all comments at the top if you have any..

Selfesteem answered 1/1, 2013 at 13:47 Comment(0)
R
0

I also got into that problem. I was using comment as the first tag it wasn't working.After I removed that it worked. Used this: {% comment %} inheriting the base html {% endcomment %}

To describe what I was doing.Got error. Removed this and used extend as the first template tag.Worked!!!!

Riojas answered 25/6, 2021 at 7:44 Comment(0)
L
0

Always remember to mention the {% extends '<TEMPLATE_NAME>' %} in the first line itself, don't even try to put comments on the first line. This will surely resolve the error!

Lobell answered 28/12, 2021 at 17:14 Comment(0)
M
0

I got the same error below:

<ExtendsNode: extends "base.html"> must be the first tag in the template.

Because I used {% extends %} after {% comment %} as shown below:

# "templates/index.html"

{% comment %} <h1>Hello World</h1> {% endcomment %}
{% extends "base.html" %}

So, I used {% extends %} before {% comment %} as shown below, then the same error was solved:

# "templates/index.html"

{% extends "base.html" %}
{% comment %} <h1>Hello World</h1> {% endcomment %}

{% extends %} is explained in Template inheritance as shown below:

  • If you use {% extends %} in a template, it must be the first template tag in that template. Template inheritance won’t work, otherwise.

In addition, you can use {% extends %} after {# #} which is a single line comment and html tags without any error as shown below. *{# #} is not a tag but is a comment syntax in Django Templates:

# "templates/index.html"

{# This is a single line comment #}
<h1>Hello World</h1>
{% extends "base.html" %}
Mudlark answered 16/5, 2023 at 2:39 Comment(0)
A
0

Remove all comments at the top of the file and place {% extends 'base.html' %} at the 1st line.

Anaesthesiology answered 5/4 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.