Python 无效的块标记:'endblock'。您是否忘记注册或加载此标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39935578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invalid block tag : 'endblock'. Did you forget to register or load this tag?
提问by ?mer sar?
l get stuck in this error. l'm fresh user of Djangoand 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:
我陷入了这个错误。我是新用户,我正在Django按照 Youtube 频道上的步骤学习它。我做了所有相同的事情,但我遇到了这个块标记错误。这是 layout1 html 内容:
<!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:
索引html内容:
{% 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:
views.py 内容:
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.
我该如何处理这个问题?因为我做了仔细检查以确保所有内容都像 Youtube 频道一样输入,通常情况下,我没有找到我做错的地方。
回答by Daniel Roseman
Django didn't recognise your starting block tag, because you have a space between the {and the %.
Django 无法识别您的起始块标记,因为您在{和之间有一个空格%。
You also have the same error in both start and end tags in the other template file.
您在另一个模板文件中的开始和结束标记中也有相同的错误。
回答by vishes_shell
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 htmlfiles, it makes code more readable.
注意:不要忘记html文件中的标识,它使代码更具可读性。
回答by Felipe Alarcon
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.
您最有可能使用 base.html 文件并将静态 css 加载到顶部{% load static %},而我的问题是我还需要将它加载到我的其他模板文件中。
I'm using Django 2.0.3 and this solved the issue for me.
我正在使用 Django 2.0.3,这为我解决了这个问题。
回答by excyberlabber
For me it was emacs breaking the lines up when I copied the template over, so
对我来说,当我复制模板时,emacs 打破了线路,所以
{% endif
was on one line and
在一条线上并且
%}
was on the next line. These need to be together on one line, and
在下一行。这些需要在一条线上,并且
{{ variable_name }}
too.
也。
回答by Mohammad Khatibzadeh
For me the problem was with {% extends %}, it was extend.
对我来说,问题出在{% extends %},它是扩展。
回答by Cryce Truly
In your Html template, ensure you {% load static %}just after your {% block content %}before using it in the template
在你的HTML模板,确保你{% load static %}只是你以后{% block content %}在模板中使用它之前,
回答by Shoaib Malek
In my case,
{% csrf token %}instead of this {% csrf_token %}solved.
在我的情况下,
{% csrf token %}而不是这个{% csrf_token %}解决了。
If you are missed _ (underscore) in code then also you will got this error.
如果您在代码中遗漏了 _(下划线),那么您也会收到此错误。
回答by HassanSh__3571619
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:
不是要回答这个特定问题,而是要分享我的案例,该案例给出了相同的错误:在我的案例中,由于我忘记在链接引用名称中添加 url,因此导致了同样的错误:
href="{% url 'allblogs' %}". worked.
while error was received when url was missed like below:
当 url 丢失时收到错误,如下所示:
href="{% 'allblogs' %}". Error been generated.
回答by Gal Bracha
For me it was the issue of using i18nwithout puttingthe {% load i18n %}inside the template file (I only put it in the base template)
对我来说,这是使用的问题,i18n没有把该{% load i18n %}模板文件中(我只把它的基本模板)
回答by Claudio Finizio
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 boththe including and in the included templates.
与@gal-bracha 的回答类似,对我来说,问题是由于在另一个模板中包含一个模板(通过{% include %}标签)并且两个模板都使用了{% trans "some text" %}标签。在这种情况下,{% load i18n %}必须写在双方的包括在包含的模板。

