python-2.7 - 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?
解决方案
关键字end
和之间不应有空格。block
endblock
改变:
{%end block%}
到:
{% endblock %}
推荐阅读
- python - 无法在本地调试 github 项目,但可以安装并作为程序运行,由于导入问题而出错
- c# - 单选按钮在我的 Windows 窗体应用程序中第一次单击时没有反应
- python - 如何计算 K 折交叉验证中所有折的分类报告中的平均值?
- electron - 为什么电子不创建文件?
- xml - Paraview:以 VTK-XML 存储表示
- dask - 有效地将每个组的附加信息传递给 Dask dataframe.groupby.apply 函数
- python-3.x - 在 Windows 10 上使用 tkinter 在后台为 GUI 运行控制台窗口
- python - Pandas:基于列名的列中位数
- android - Android Kotlin 协程异常
- python - 删除某个字符后的 n 个字符