0

我有一个有两个模板变量的模板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>
4

2 回答 2

1

解决这个问题的最好方法可能是确保使用模板的视图只使用相同的变量名。

如果那不可能,我会这样做:

{% if list_of_books or search_results %}
    <table border="0" align="left" width="70%"> 
    {% if search_results %}
        {% include "book_listing.html" with books=search_results %}
    {% else %}    
        {% include "book_listing.html" with books=list_of_books %}
    {% endif %}
    </table>
{% else %}
    <p>No Books available.</p>
{% endif %}

然后把它放进去book_listing.html

{% for book in books %}
  <tr>
    <td>  
      <a href="{{ book.get_absolute_url }}">{{ book.title }}</a>
    </td>
  </tr>
{% endfor %}
于 2013-08-12T13:35:14.583 回答
0
<span>
    {%if booking.DiscountedFare == booking.TotalFare %}
        Rs.{{ booking.TotalFare }}

    {% else %}   
        Rs.{{ booking.DiscountedFare }}         

    {% endif %}
</span>
于 2015-08-12T09:16:26.040 回答