我是 html 模板和 Django 的初学者。
author_list.html
{% extends "base_generic.html" %}
{% block content %}
<h1>All authors</h1>
<ul>
{% for author in author_list %}
<li><a href="">{{ author.last_name }}:</a> {% for book in author.book_set.all %} {{ book.title }} {% if not
forloop.last %}, {% endif %}{% endfor %}</li>
{% endfor %}
</ul>
{% endblock %}
错误: 第 7 行的 block 标记无效:“endif”,应为“empty”或“endfor”。您是否忘记注册或加载此标签?
我有代码块。它在没有阻塞的情况下工作,但不适用于条件。如何修复它。帮助!我应该在每个书名后粘贴“,”。
最佳答案
您不应该在多行中编写模板标签。模板标签应该在同一行开始({%
)和结束(%}
)。否则 Django 模板语言解析器将对此出错。因此,您应该将其重写为:
{% extends "base_generic.html" %}
{% block content %}
<h1>All authors</h1>
<ul>
{% for author in author_list %}
<!-- same line ↓ -->
<li><a href="">{{ author.last_name }}:</a> {% for book in author.book_set.all %} {{ book.title }} {% if not forloop.last %}, {% endif %}{% endfor %}</li>
{% endfor %}
</ul>
{% endblock %}
关于python - 第 7 行 : 'endif' , 上的无效 block 标记应为 'empty' 或 'endfor'。您是否忘记注册或加载此标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69675117/