0
votes

I have a template which has two template variables list_of_books, search_results

First i am checking whether results exists for any one of the variables , if not exists it should display Books not available as below

Next if search_results variable has results, we should loop and print the data, else if list_of_books variable has results , we should loop and print the data

{% 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 %}

But by the above html code i am getting the below error, actually where i am making mistake in writing template tags ?

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>
2
I would avoid this type of logic in your template. Instead, assign the objects server-side and then iterate over a single collection. - Brandon
I used this because i may have to display the many common fields in future so looking by this way, can u please let me now how to do this ? - Shiva Krishna Bavandla
Django template engine doesn't let you define start of condition or loop like this. - Bibhas Debnath
If your objects share common fields, it makes even more sense to assign them to a variable named simply "objects" and doing the if statement server-side. As @Bibhas said, you can't define the start condition of a for loop in the template as you have done. - Brandon
If the html part is almost same, I'd suggest you create a small snippet in a html file and include that file with the list of books. - Bibhas Debnath

2 Answers

1
votes

The best way to solve this is probably to make sure that views which use the template just use the same variable name.

If that's not possible I'd do this instead:

{% 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 %}

And then put this in book_listing.html:

{% for book in books %}
  <tr>
    <td>  
      <a href="{{ book.get_absolute_url }}">{{ book.title }}</a>
    </td>
  </tr>
{% endfor %}
0
votes
<span>
    {%if booking.DiscountedFare == booking.TotalFare %}
        Rs.{{ booking.TotalFare }}

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

    {% endif %}
</span>