出现Django模板语法错误。我该如何解决?

huangapple go评论71阅读模式
英文:

Getting A Django Template Syntax Error. How do I solve this?

问题

我尝试在index.html中扩展base.html文件。 但是我一直收到以下错误消息:
在第2行上的无效块标签:'endblock'。 您是否忘记注册或加载此标签?

base.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>{% block title %}{% endblock %}</title>
        <!-- {% block css_files %} {% endblock %} -->
    </head>
    <body>
        {% block content %}{% endblock %}
    </body>
</html>

index.html

{% extends 'base.html' %} {% block title %} My Blog {% endblock %} {% block
content %} Hello {% endblock %}

我确保了大括号和百分号之间没有任何间隔。 问题可能出在哪里?

英文:

I tried to extend the base.html file in index.html. But I keep getting the following error:
Invalid block tag on line 2: 'endblock'. Did you forget to register or load this tag?

base.html

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
	&lt;head&gt;
		&lt;meta charset=&quot;UTF-8&quot; /&gt;
		&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; /&gt;
		&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
		&lt;title&gt;{% block title %}{% endblock %}&lt;/title&gt;
		&lt;!-- {% block css_files %} {% endblock %} --&gt;
	&lt;/head&gt;
	&lt;body&gt;
		{% block content %}{% endblock %}
	&lt;/body&gt;
&lt;/html&gt;

index.html

{% extends &#39;base.html&#39; %} {% block title %} My Blog {% endblock %} {% block
content %} Hello {% endblock %}

I've made sure there are no gaps between any of the curly braces and %. Where could be the issue?

答案1

得分: 1

{% extends 'base.html' %}

{% block title %}我的博客{% endblock %}

{% block content %}你好{% endblock %}
英文:

You can't have a newline in the middle of a tag, start each block tag on a separate line

{% extends &#39;base.html&#39; %}

{% block title %}My Blog{% endblock %}

{% block content %}Hello{% endblock %}

huangapple
  • 本文由 发表于 2023年3月9日 19:27:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定