Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiplication in django template without using manually created template tag

I want to achieve multiplication operation in django template. For example I have the values, price=10.50 quantity=3

With the help of this link

http://slacy.com/blog/2010/07/using-djangos-widthratio-template-tag-for-multiplication-division/

i tried below codes for achieving it,

{% widthratio quantity 1 price %}

but its returning only 31. But i need the answer in float (31.5)

And i want to achieve it without using the manually created tags

How can i achieve it? Thanks in advance...

like image 597
Sakeer Avatar asked Aug 21 '13 06:08

Sakeer


People also ask

How do I use custom template tags in Django?

Building Tags and Filters Custom tags and filters live in your Django app in a templatetags/ directory. You can import any files in this directory into a template using the {% load %} tag. The name of the module you create will be the name you use to load the tag library.

How do you use math operation in Django template?

Use django-mathfilters. In addition to the built-in add filter, it provides filters to subtract, multiply, divide, and take the absolute value. For the specific example above, you would use {{ 100|sub:object. article.


4 Answers

You can use the built-in widthratio template tag.

  • a*b use {% widthratio a 1 b %}
  • a/b use {% widthratio a b 1 %}

Note: the results are rounded to an integer before returning.

@see https://docs.djangoproject.com/en/dev/ref/templates/builtins/

like image 108
hahakubile Avatar answered Sep 28 '22 14:09

hahakubile


There are 2 approaches:

  • Computing the values inside the view and pass them to the template (recommended in my opinion)
  • Using template filters

In the manner of the add filter, you could always create your own multiply filter, creating your own custom filter:

from django import template

register = template.Library()

@register.filter
def multiply(value, arg):
    return value * arg

Then in your template, something like that should work.

{{ quantity | multiply:price }}

This is not tested, and I never did this as - again - I find neater to compute datas inside the views and render only with the templates.

like image 24
Mathieu Marques Avatar answered Sep 29 '22 14:09

Mathieu Marques


Another approach that I have used seems cleaner to me. If you are going through a queryset, it doesn't make sense to compute the values in your view. Instead, add the calculation as a function in your model!

Let's say your model looks like this:

Class LineItem:
    product = models.ForeignKey(Product)
    quantity = models.IntegerField()
    price = models.DecimalField(decimal_places=2)

Simply add the following to the model:

    def line_total(self):
        return self.quantity * self.price

Now you can simply treat line_total as if it were a field in the record:

{{ line_item.line_total }}

This allows the line_total value to be used anywhere, either in templates or views, and ensures that it is always consistent, without taking up space in the database.

like image 29
WahhabB Avatar answered Sep 29 '22 14:09

WahhabB


I know it's been so long since this question came out but, now there's a library called django-mathfilters which made mathematical operations easier in Django templates. you can easily write <li>42 * 0.5 = {{ answer|mul:0.5 }}</li> for multiplication.

check it out https://pypi.org/project/django-mathfilters/

like image 26
Mai Elshiashi Avatar answered Oct 01 '22 14:10

Mai Elshiashi