How to use if else condition using mathfilters in Django?
I have a question, I'm doing a project in Django and there is a part of the application called Pagos(payments), there I show the following: payments made, total_pagos(total payment) and pending payment,
To show the information of these payments I'm using mathfilters because addition and subtraction are used in my templates.
For the template it's showing the information properly, only the problem comes when I want to use conditionals since once pending payment is equal to 0.00 it means that the client already pays everything and must show a text that says “PAGADO” (paid), when pending payment it has another number than 0.00 must show a label that says “PENDIENTE” (pending).
This is the code that I am using and it works, since it shows me the result of the costo_servicio (amount of the service) cost minus the total_pagos (total payments), whatever it shows is the pending payment, it can show several numbers or 0.00:
{% for juicio in juicios %}
{{ juicio.costo_servicio|sub:total_pagos }}
{% endfor %}
But if I put this, it doesn't show the if and therefore doesn't do the subtraction or show the text PAGADO(paid):
{% for juicio in juicios %}
{% if juicio.costo_servicio|sub:total_pagos == 0.00 %}
{{ juicio.costo_servicio|sub:total_pagos }}
<td class="text-right"><span class="saldo-pagado" ><strong>PAGADO</strong></span></td>
{% endif %}
{% endfor %}
I'll share you the rest of my models and my views:
juicios models.py
class Juicios(models.Model):
cliente = models.ForeignKey(Cliente, on_delete=models.CASCADE)
no_expediente_cv = models.CharField(max_length=100)
costo_servicio=models.DecimalField(max_digits=10, decimal_places=2)
pagos models.py
class Pagos(models.Model):
cliente = models.ForeignKey(Cliente, on_delete=models.CASCADE)
monto_pago=models.IntegerField()
pagos view.py
def PagosDetalle(request, cliente_id):
context = {}
cliente = Cliente.objects.get(id=cliente_id)
pagos = Pagos.objects.filter(cliente=cliente_id)
juicios = Juicios.objects.filter(cliente=cliente_id)
total_pagos = Pagos.objects.filter(cliente=cliente).aggregate(sum_of_all_orders=Sum('monto_pago'))
pagos_filtrados=Pagos.objects.filter(cliente=cliente)
context["pagos"] = pagos
context["pagos_filtrados"] = pagos_filtrados
context["total_pagos"] = total_pagos['sum_of_all_orders']
if juicios:
context["juicios"] = juicios
return render(request, "pagos-detalle.html",context)
You can use the with
tag to store the result of the calculation in a template variable, for example:
{% with juicio.costo_servicio|sub:total_pagos as result %}
{% if result == 0 %}
...
{% endif %}
{% endwith %}