这是一个很小、很容易忽略但却不易查找的错误,困扰了我好几个小时,原代码如下:
Django项目中templates下的html模板index.html
-
<!DOCTYPE html> -
<html lang="en"> -
<head> -
<meta charset="UTF-8"> -
<title>Title</title> -
</head> -
<body> -
<a href="{% url 'booktest:show' '456' '789'%}">展示</a> -
<hr> -
{{hero.showname}} -
<hr> -
奇数行显示为蓝色,偶数行显示为红色 -
<hr> -
<ul> -
{% for hero in list %} -
{% if forloop.counter|divisibleby:"2" % } -
<li style="color:red">{{ forloop.counter }}: {{ hero.showname }}</li> -
{% else %} -
<li style="color:blue">{{ forloop.counter }}: {{ hero.showname }}</li> -
{% endif %} -
{% empty %} -
<li>啥也没找到</li> -
{% endfor %} -
</ul> -
</body> -
</html>
运行一直报如下错误:
图二中18行变红,但是看了几遍逻辑关系都没有问题,后来受一篇相似错误文章的启发,检查到第16行,原来错误在于此处:
对,就是第二个百分号和右花括号中间多了一个空格,去掉空格后,问题顺利解决,不再报错!
好吧,以后敲代码一定要仔细。
参考文章:https://stackoverflow.com/questions/27215623/templatesyntaxerror-invalid-block-tag-else-expected-endif