Returning the minimum value for comparison in Django template

Asked

Viewed 155 times

3

Following a reply on /a/96503/761

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 passage

minimos = {}

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

returns in the template the following result:

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

returns:

{'maize 500 g': Decimal('1.32'), 'chocolate powder 500 g': Decimal('2.92'), 'soap 1 pcte': Decimal('1.87'), 'beer 350 ml': Decimal('2.49'), 'detergent 500 ml': Decimal('2.26'), 'chickpeas 500 g': Decimal('2.21'), 'soap in bar 100 g': Decimal('2.97'), 'mozzarella cheese 500 g': Decimal('1.86'), 'beans 1 Kg': Decimal('4.43'), 'disinfectant 1 l': Decimal('3.58'), 'absorbent 1 pcte': Decimal('2.68'), 'whole bread 1 pcte': Decimal('2.41'), 'toothpaste 1 unid': Decimal('1.34'), 'refrigerant 350 ml': Decimal('2.81')'integral juice 1 l': Decimal('3.99'), 'peanut 500 g': Decimal('2.41'), 'coffee powder 1 Kg': Decimal('1.65'), 'mineral water 1.5 l': Decimal('2.22'), 'refrigerant 1.5 l': Decimal('4.75'), 'bleach 1 l': Decimal('3.72'), 'milk 1 l': 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')} {'maize 500 g': Decimal('1.32'), 'chocolate powder 500 g': Decimal('2.92'), 'soap 1 pcte': Decimal('1.87'), 'beer 350 ml': Decimal('2.49'), 'detergent 500 ml': Decimal('2.26'), 'chickpeas 500 g': Decimal('2.21'), 'soap bar 100 g': Decimal('2.97'), 'mozzarella cheese 500 g': Decimal('1.86'), 'beans 1 Kg': Decimal('4.43'), 'disinfectant 1 l': Decimal('3.58'), 'absorbent 1 pcte': Decimal('2.68'), 'whole bread 1 pcte': Decimal('2.41'), 'toothpaste 1 unid': Decimal('1.34'), 'refrigerant 350 ml': Decimal('2.81'), 'integral juice 1 l': Decimal ('3.99'), 'peanut 500 g': Decimal ('2.41'), 'coffee powder 1 Kg': Decimal('1.65'), 'mineral water 1.5 l': Decimal('2.22'), 'refrigerant 1.5 l': Decimal('4.75'), 'bleach 1 l': Decimal('3.72'), 'milk 1 l': Decimal('1.63'), 'rice 5 Kg': decimal ('3.16'), 'nappy 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, ie, this is the lowest value of each product line, ie the cheapest product.

So 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 getting the lowest value back correctly from the minimum dictionary to do this.

How to fix this?

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

Expected result

inserir a descrição da imagem aqui

1 answer

2


You can’t make one zip in the template (i.e. iterate over two or more lists simultaneously), so I suggest doing this also in the view before sending it there:

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 %}
  • mgibsonbr Thank you.

  • I left an image to share with everyone the expected result, and achieved. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.