我有一个有两个模板变量的模板list_of_books, search_results
首先,我正在检查任何一个变量的结果是否存在,如果不存在,它应该显示Books not available
如下
接下来如果search_results
变量有结果,我们应该循环并打印数据,否则如果list_of_books
变量有结果,我们应该循环并打印数据
{% if list_of_books or search_results %}
<table border="0" align="left" width="70%">
{% if search_results %}
{% for book in search_results %}
{% else %}
{% for book in list_of_books %}
{% endif %}
<tr>
<td>
<a href="{{ book.get_absolute_url }}">{{ book.title }}</a>
</td>
</tr>
{% endfor %}
</table>
{% else %}
<p>No Books available.</p>
{% endif %}
但是通过上面的 html 代码,我得到了以下错误,实际上我在编写模板标签时出错了?
Error during template rendering
In template /home/virtualenvironment/apps/books/templates/books/list_of_books.html, error at line 23
Invalid block tag: 'else', expected 'empty' or 'endfor'
20 <table border="0" align="left" width="70%">
21 {% if search_results %}
22 {% for book in search_results %}
23 {% else %}
24 {% for book in list_of_books %}
25 {% endif %}
26 <tr>
27 <td>
28 <a href="{{ book.get_absolute_url }}">{{ book.title }}</a>
29 </td>
30 </tr>
31 {% endfor %}
32 </table>