Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add a style in css depending on the value of the field with django

I’m trying to style my line of code:

<td class="{% if inven < 10 %}color-green-primary{% else %}color-red-primary{% endif %}">{{inven.quantityInventory}}</span></td>

It should change color depending on the value that is presented.
The bad thing is that it always shows it to me in red (in else). what am I doing wrong?

html

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

                             {% for inven in inventory %}
                                <tr>
                                   <td><strong>{{inven.codigoInventory}}</strong></td>
                                   <td>{{inven.descriptionInventory}}</td>
                                   <td>${{inven.unitPriceInventory}}</td>
                                   <td class="{% if inven < 10 %}color-green-primary{% else %}color-red-primary{% endif %}">{{inven.quantityInventory}}</span></td>
                                   <td>{{inven.dealer}}</td>
                                   <td>{{inven.invoiceNumber}}</td>
                                   <td>
                                      <div class="badge bg-soft-danger font-size-12">{{inven.status}}</div>
                                   </td>
                                   <td><a href="{% url 'inventory:inventory_detail' inven.id%}">Detail</a></td>
                                   <td><a href="{% url 'inventory:edit_inventory' inven.id%}">Edit</a></td>
                                   <td><a href="{% url 'inventory:eliminar_inventory' inven.id%}" class="text-danger" >Delete</a></td>
                                </tr>
                                {% endfor %}

views.py

def list_inventory(request):

    if request.method == 'POST':
        fromdate=request.POST.get('fromdate')
        todate = request.POST.get('todate')
        searchresult = Inventory.objects.filter(fecha_registro__range=(fromdate, todate))
        return render(request,'inventory/inventory-list.html',{'inventory':searchresult})

    else:
        displaydata = Inventory.objects.all()
    return render(request, 'inventory/inventory-list.html', {'inventory': displaydata})

>Solution :

You should be comparing against inven.quantityInventory not just inven. It does not make sense to compare a model instance with an int, compare against an int attribute

<td class="{% if inven.quantityInventory < 10 %}color-green-primary{% else %}color-red-primary{% endif %}">{{ inven.quantityInventory }}</span></td>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading