我正在尝试在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?
答案 0 :(得分:1)
关键字end
中block
和endblock
之间不应有空格。
更改:
{%end block%}
收件人:
{% endblock %}