首页 > 解决方案 > TemplateSyntaxError:第 25 行的块标记无效:'end',预期为 'endblock'。您是否忘记注册或加载此标签?

问题描述

我正在尝试加载main_template.html代码hello.html。但是显示一些错误来注册或加载标签。我该如何解决?

myapp.views.py

def hello(request):
# return HttpResponse("welcome to my app")
today = datetime.datetime.now().date
daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
return render(request,"hello.html",{"today":today,"days_of_week" : daysOfWeek})

myproject.templates.hello.html

{% extends "main_template.html" %}

{% block title %}My Hello Page{% endblock %}

{% block content %}

hello world!!! <p>today is {{today}}</p>
we are

{% if today.day is 1 %}
the first day of the month

{%elif today.day is 30 %}
the last day of the month

{%else %}
i dont know

{%endif%}
<p>
     {% for day in days_of_week %}
     {{day}}
  </p>

{% endfor %}
{% endblock %}

myproject.templates.main_template.html

<html>
<head>
<title>
    {% block title %}Page Title{%end block%}
</title>
</head>
<body>
{% block content %}
Body Content
{% end block%}
</body>
</html>

发生了错误:

Request Method: GET
Request URL:    http://localhost:8000/myapp/hello/
Django Version: 1.11.20
Exception Type: TemplateSyntaxError
Exception Value:    
Invalid block tag on line 4: 'end', expected 'endblock'. Did you forget to register or load this tag?
Exception Location: /home/divum/PycharmProjects/untitled3/venv/local/lib/python2.7/site-packages/django/template/base.py in parse, line 515
Python Executable:  /home/divum/PycharmProjects/untitled3/venv/bin/python2.7

Python 版本:2.7.14

TemplateSyntaxError: Invalid block tag on line 4: 'end', expected 'endblock'. Did you forget to register or load this tag?

标签: python-2.7django-rest-framework

解决方案


关键字end和之间不应有空格。blockendblock

改变:

{%end block%}

到:

{% endblock %}

推荐阅读