Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 565
ömer sarı Avatar asked Oct 08 '16 17:10

ömer sarı


4 Answers

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.

like image 131
Daniel Roseman Avatar answered Nov 15 '22 12:11

Daniel Roseman


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.

like image 40
vishes_shell Avatar answered Nov 15 '22 13:11

vishes_shell


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.

like image 10
Felipe Alarcon Avatar answered Nov 15 '22 11:11

Felipe Alarcon


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.

like image 3
excyberlabber Avatar answered Nov 15 '22 11:11

excyberlabber