We have a collection of products . Each product has several prices for different sites product.prices . There is also a collection with sites sites . The task: to form a table, where the row is the item, the column is the site. In the cell - the price corresponding to each site.

 Наименование | Цена сайта 1 | Цена сайта 2 | Цена сайта 3 

We try to display the table:

 <table class="table"> {% for product in products %} <tr> <td>{{ product.name }}</td> {% for site in sites %} {% for price in product.prices %} {% if price.site == site %} <td>{{ price.price }}</td> {% endif %} {% endfor %} {% endfor %} </tr> {% endfor %} </table> 

The problem is that the product does not always have prices for each of the sites. And in such cases, the lines are shifted to the left. How to implement a table without moving rows?

    1 answer 1

    Insert a cell in any case, is there a price or not:

     <table class="table"> {% for product in products %} <tr> <td>{{ product.name }}</td> {% for site in sites %} <td> {% for price in product.prices %} {{ price.site == site ? price.price : ''}} {% endfor %} </td> {% endfor %} </tr> {% endfor %} </table> 
    • It would be too easy :) In this case, with each pass of each cycle, when the price of a site’s product is missing, an empty cell is added. This adds extra empty cells. - Pavel Sokolov
    • After editing: also did not work. Anyway, the left shift is saved. - Pavel Sokolov
    • I did not understand, this is the table, what is the indentation? empty cells must be. Show your left shift. - Artem Gorlachev
    • I vaguely remember that it was impossible to make a completely empty cell, set it up &nbsp; - Artem Gorlachev
    • I beg your pardon, right. This is what happens with your take.ms/aaqFP code & nbsp; did not help - Pavel Sokolov