Django报错:TemplateSyntaxError at / Invalid block tag: 'else', expected 'empty' or 'endfor'

本文介绍了一次在Django项目中遇到的模板语法错误,详细分析了由于百分号与右花括号间多出的空格导致的问题,并分享了解决方案。

这是一个很小、很容易忽略但却不易查找的错误,困扰了我好几个小时,原代码如下:

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


TemplateSyntaxError at /index/ Invalid block tag on line 134: 'ifequal', expected 'empty' or 'endfor'. Did you forget to register or load this tag? Request Method: GET Request URL: http://localhost:8000/index/ Django Version: 5.2.4 Exception Type: TemplateSyntaxError Exception Value: Invalid block tag on line 134: 'ifequal', expected 'empty' or 'endfor'. Did you forget to register or load this tag? Exception Location: C:\Users\armin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\django\template\base.py, line 567, in invalid_block_tag Raised during: newswebsite.views.IndexView Python Executable: C:\Users\armin\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe Python Version: 3.11.9 Python Path: ['C:\\Users\\armin\\Desktop\\newswebsite', 'C:\\Users\\armin\\Desktop\\newswebsite', 'E:\\Program Files\\JetBrains\\PyCharm ' '2024.1\\plugins\\python\\helpers\\pycharm_display', 'C:\\Program ' 'Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\\python311.zip', 'C:\\Program ' 'Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\\DLLs', 'C:\\Program ' 'Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\\Lib', 'C:\\Program ' 'Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0', 'C:\\Users\\armin\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages', 'C:\\Program ' 'Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\\Lib\\site-packages', 'E:\\Program Files\\JetBrains\\PyCharm ' '2024.1\\plugins\\python\\helpers\\pycharm_matplotlib_backend'] Server time: Tue, 26 Aug 2025 11:25:46 +0000 Error during template rendering In template C:\Users\armin\Desktop\newswebsite\newswebsite\templates\index.html, error at line 134
08-27
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值