Home > Blockchain >  How to use inputs to do calculations with mathfilters in Django?
How to use inputs to do calculations with mathfilters in Django?

Time:11-30

I am using mathfilters in Django, I want to do a multiplication between quantity and unit_price and reflect it in total_price. I already followed the documentation from here mathfilters and when implementing it as it says and filling those 2 inputs with numbers, the result is not reflected anywhere

Maybe I am not implementing it well but when I search I see that there is very little information to use mathfilters

the result of the operation is not reflected

<td>
   {{presupuestosparteform.quantity}}
</td>
<td>
   {{presupuestosparteform.unit_price}}
</td>
<td>
  <li>{{ presupuestosparteform.quantity|mul:presupuestosparteform.unit_price }}</li>
</td>



CodePudding user response:

Pls attach your view file

You are using the django-mathfilter in the right way. I thing there must be some problem with your values you are providing over there

  • check the type of {presupuestosparteform.quantity} and presupuestosparteform.unit_price
  • the value of presupuestosparteform.unit_price and presupuestosparteform.unit_price must be of int or float type .
  • check out the python version must be Python 3.5

CodePudding user response:

I recommend using a model @property. This will enable you to make further calculation if needed like deducting discounts etc. Just add the following property directly to your model:

@property
def total_price(self):
    return self.quantity * self.unit_price
  • Related