'Invalid block tag : 'endblock'. Did you forget to register or load this tag?
l get stuck in this error. l'm fresh user of Django
and l m learning it by following steps on Youtube channel. l did everything same but l got this block tag error.
here is layout1 html content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{ % block title %}{% endblock %}</title>
</head>
<body>
{ % block content %} {% endblock %}
</body>
</html>
index html content:
{% extends "layout/layout1.html"%}
{% block title %}The Video page{% endblock %}
{ % block content %}
<h1>This is a html</h1>
<p>This is a p tag</p>
<a href="http://www.noobmovies.com">Click me!</a>
<img src="https://upload.wikimedia.org/wikipedia/en/7/72/Anthony_Raneri.jpg"/>
{% endblock % }
views.py content:
from django.template.response import TemplateResponse
# Create your views here.
def video(request):
return TemplateResponse (request,"video/index.html",{})
how can l handle this problem? as l did double-check to make sure everything is typed same like Youtube channel and normally ,l did not get where l did a mistake.
Solution 1:[1]
Django didn't recognise your starting block tag, because you have a space between the {
and the %
.
You also have the same error in both start and end tags in the other template file.
Solution 2:[2]
You simply have typos.
You should have {%
not { %
, and you got those typos in both of templates.
So you need to have
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block content %} {% endblock %}
</body>
</html>
and
{% extends "layout/layout1.html"%}
{% block title %}The Video page{% endblock %}
{% block content %}
<h1>This is a html</h1>
<p>This is a p tag</p>
<a href="http://www.noobmovies.com">Click me!</a>
<img src="https://upload.wikimedia.org/wikipedia/en/7/72/Anthony_Raneri.jpg"/>
{% endblock %}
NOTE: don't forget about identations in html files, it makes code more readable.
Solution 3:[3]
If none of the previous answers worked for you, try the following:
You are most likely using a base.html file and have the static css being loaded on the top {% load static %}
and the problem for me was that I needed to also load this in my other template file.
I'm using Django 2.0.3 and this solved the issue for me.
Solution 4:[4]
For me it was emacs breaking the lines up when I copied the template over, so
{% endif
was on one line and
%}
was on the next line. These need to be together on one line, and
{{ variable_name }}
too.
Solution 5:[5]
For me it was the issue of using i18n
without putting the {% load i18n %}
inside the template file (I only put it in the base template)
Solution 6:[6]
For me the problem was with {% extends %}
, it was extend.
Solution 7:[7]
In your Html template, ensure you {% load static %}
just after your {% block content %}
before using it in the template
Solution 8:[8]
In my case,
{% csrf token %}
instead of this {% csrf_token %}
solved.
If you are missed _ (underscore) in code then also you will got this error.
Solution 9:[9]
I got this error because I mixed up {% %}
and {{ }}
. I had:
<title>{% user.name %} ({% user.id %})</title>
When I should have had:
<title>{{ user.name }} ({{ user.id }})</title>
Solution 10:[10]
in my case use {% load static %}
after {% extends 'base.html' %}
solved.
Solution 11:[11]
Similar to @gal-bracha answer, for me the problem was caused by including a template inside another (through the {% include %}
tag) and both templates making use of a {% trans "some text" %}
tag. In this case, the {% load i18n %}
must be written in both the including and in the included templates.
Solution 12:[12]
Not to answer this specific question, but to share my case which gave the same error: In my case this same error been caused because I forgot to add the url in the link reference name:
href="{% url 'allblogs' %}". worked.
while error was received when url was missed like below:
href="{% 'allblogs' %}". Error been generated.
Solution 13:[13]
if you encounter this:
Invalid block tag on line 172: 'endfor'. Did you forget to register or load this tag?
Check:
Any part of your code commented, check if {% endfor %}
has been repeated.
If it is repeated delete one especially the commented section.
Thank you.
Solution 14:[14]
in my case i used replaced {% csrf token %}
with {% csrf_token %}
and it seemed to work.
In any template that uses a POST
form, use the csrf_token
tag inside the <form>
element if the form is for an internal URL, e.g.:
<form method="post">{% csrf_token %}
This should not be done for POST
forms that target external URLs, since that would cause the CSRF token to be leaked, leading to a vulnerability.
Solution 15:[15]
This also happens when you forget putting your phrase between quotes.
{% trans My phrase %}
instead of
{% trans "My phrase" %}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow