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
mgibsonbr Thank you.
– Regis Santos
I left an image to share with everyone the expected result, and achieved. Thank you.
– Regis Santos