Returning the minimum value for comparison in template Django

3

Following up on an answer at link

def quotation_list(request):
    stores = list(Store.objects.all())
    products = list(Product.objects.all())
    # indice
    index_store = {store.id: index for index, store in enumerate(stores)}
    index_product = {product.id: index for index,
                     product in enumerate(products)}
    # dados para o template
    cabecalho = ["Lojas"] + [store.store for store in stores]
    linhas = [([product.product] + [None for store in stores])
              for product in products]

    for pev in Quotation.objects.all():
        linhas[index_product[pev.product_id]][
            index_store[pev.store_id] + 1] = pev.price

    minimos = {}

    for linha in linhas:
        minimos[linha[0]] = min(linha[1:])

    context = {'cabecalho': cabecalho, 'linhas': linhas, 'minimos': minimos}
    return render(request, 'core/quotation_list.html', context)

This stretch

minimos = {}

for linha in linhas:
    minimos[linha[0]] = min(linha[1:])

returns the following result in the template:

{% for linha in linhas %}
    <tr>
        <td>{{ minimos }}</td>
    </tr>
{% endfor %}

returns:

Decimal ('1.32'), 'chocolate 500 g': Decimal ('2.92'), 'soap 1 pcte': Decimal ('1.87'), '350 ml beer' : Decimal ('2.49'), '500 ml detergent': Decimal ('2.26'), '500 g' bead: Decimal ('2.21'), 'Decimal 1': Decimal ('4.83'), 'Decimal 1': Decimal ('3.58'), 'absorbent 1 pc': Decimal (' ('2.41'), 'whole-grain bread 1': Decimal ('2.41'), '1' toothpaste: Decimal ('1.34'), '350 ml refrigerant' 'Decimal (' 3.99 '),' Peanut 500 g ': Decimal (' 2.41 '),' Coffee powder 1 kg ': Decimal (' 1.65 '),' mineral water 1.5 l ' ('3.75'), 'milk 1 l': Decimal ('1.63'), 'rice 5 kg': Decimal ('4.75'), 3.16 '), diaper M 1 tablet: Decimal (' 1.54 '),' powdered soap 1 kg ': Decimal (' 3.58 '),' shampoo 350 ml ' Decimal ('1.32'), '350 ml beer': Decimal ('1.32'), Decimal ('2.32'), ('2.26'), '500 ml detergent': Decimal ('2.26'), '500 g': Decimal ('2.21'), '100 g detergent' 'Decimal (' 2.68 '), Decimal (' 2.86 '), Decimal (' 2.86 '), Decimal (' 4.86 '), ('2.41'), '1' toothpaste: Decimal ('1.34'), '350 ml refrigerant': Decimal ('2.81'), '1' whole juice: Decimal ('3.99'), 'peanut 500 g': Decimal ('2.41'), 'coffee powder 1 kg': Decimal ('1.65'), mineral water 1.5 l: Decimal 'Decimal (' 3.75 '),' Milk 1 ': Decimal (' 1.63 '),' Rice 5 Kg ': Decimal (' 3.16 '), , 'diaper M 1 pcte': Decimal ('1.54'), 'Soap powder 1 kg': Decimal ('3.58'), shampoo 350 ml: Decimal ('2.97')}

Question : I need to make a comparison, that is, this is the lowest value of each product line, ie the cheapest product.

Dai I tried

{% if minimos == item %}
    <td style="color: red;">{{ item }}</td>
{% else %}
    <td>{{ item }}</td>
{% endif %}


{% for item in linha %}
    <td>{{ item }}</td>
{% endfor %}

To compare the lowest value of each line and highlight it in red.

But I'm not able to correctly return the lowest value from the minimal dictionary to do this.

How do I fix this?

Again, I need to know what the lowest value of each line is and paint it red.

Expected result

    
asked by anonymous 05.12.2015 / 04:24

1 answer

2

Can not zip in template (ie iterate over two or more lists simultaneously), then I suggest doing that also in the view before sending it over:

minimos = [] # Em vez de mapear, usa o mesmo índice da linha

for linha in linhas:
    minimos.append(min(linha[1:]))

                                   # Junta linhas e mínimos numa lista de pares (tuplas)
context = {'cabecalho': cabecalho, 'linhas_minimos': zip(linhas, minimos)}

And in the template:

{% for linha, minimo in linhas_minimos %}
<tr>
    {% for item in linha %}
        {% if minimo == item %}
            <td style="color: red;">{{ item }}</td>
        {% else %}
            <td>{{ item }}</td>
        {% endif %}
    {% endfor %}
</tr>
{% endfor %}
    
05.12.2015 / 05:52