TemplateSyntaxError:第25行的块标记无效:“ end”,预期为“ endblock”。您是否忘记注册或加载此标签?

时间:2019-03-06 13:09:40

标签: python-2.7 django-rest-framework

我正在尝试在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?

1 个答案:

答案 0 :(得分:1)

关键字endblockendblock之间不应有空格。

更改:

{%end block%}

收件人:

{% endblock %}