Math filter

54 views
Skip to first unread message

sum abiut

unread,
Jul 18, 2017, 10:22:09 PM7/18/17
to django...@googlegroups.com
Hi,
needed direction maths filters on django templates. for example how to you perform this on a template.

a*b +b*c

i did      a|mul:b|add:b|mul:c    but got a wrong answer. Please point me to right direction.

cheers,

James Schneider

unread,
Jul 19, 2017, 1:51:21 AM7/19/17
to django...@googlegroups.com
I wouldn't do any sort of advanced math in the template. It should be completed in the view and added to the template context, then referenced as a template variable. The template processor has no concept of the order of operations in math. 

-James 

Vijay Khemlani

unread,
Jul 19, 2017, 8:42:51 AM7/19/17
to django...@googlegroups.com
Also I guess mathfilters just executes left-to-right

so

a|mul:b|add:b|mul:c

is (((a * b) + b) * c)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWfni0w6XZmfqrjnsrTLiWZLh0NWu9ye%2BA_Cm0NDb6x3A%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

Mike Morris

unread,
Jul 19, 2017, 6:41:04 PM7/19/17
to django...@googlegroups.com

I am a newbie, but I think the answer is that you can't do that in the template - instead, calculate the number in the view and pass it to the template in the context dictionary.

I think it's a design philosophy of Django Template Language to not support this as it muddies the border between data and presentation. They've obviously made some exceptions for the really common/easy use cases - like "|add"

There are other template languages that do support that of course; if its important enough you could look at changing the template engine...

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Mike Morris

unread,
Jul 19, 2017, 7:29:00 PM7/19/17
to django...@googlegroups.com

Of course, if they're integers you could implement the "mul" as a loop of "add"s... clumsy, but would work.

Vijay Khemlani

unread,
Jul 19, 2017, 9:21:08 PM7/19/17
to django...@googlegroups.com
I'm guessing he's using the mathfilters package


On Wed, Jul 19, 2017 at 7:28 PM, Mike Morris <mi...@musicplace.com> wrote:

Of course, if they're integers you could implement the "mul" as a loop of "add"s... clumsy, but would work.


On 07/19/2017 03:39 PM, Mike Morris wrote:

I am a newbie, but I think the answer is that you can't do that in the template - instead, calculate the number in the view and pass it to the template in the context dictionary.

I think it's a design philosophy of Django Template Language to not support this as it muddies the border between data and presentation. They've obviously made some exceptions for the really common/easy use cases - like "|add"

There are other template languages that do support that of course; if its important enough you could look at changing the template engine...




On 07/18/2017 07:21 PM, sum abiut wrote:
Hi,
needed direction maths filters on django templates. for example how to you perform this on a template.

a*b +b*c

i did      a|mul:b|add:b|mul:c    but got a wrong answer. Please point me to right direction.

cheers,
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Mike Morris

unread,
Jul 19, 2017, 10:03:40 PM7/19/17
to django...@googlegroups.com
On 07/19/2017 06:20 PM, Vijay Khemlani wrote:
> I'm guessing he's using the mathfilters package
>
> https://github.com/dbrgn/django-mathfilters

Arggghhh... my ignorance is showing! I hope I included an "I'm new here" disclaimer in the original post!!!

Anyway, never heard of it, I'll take a look!

Thank You Vijay!


Vijay Khemlani

unread,
Jul 20, 2017, 8:46:34 AM7/20/17
to django...@googlegroups.com
Don't worry, I only knew about the project because I use it in a small part of an application that can't execute computations on the view

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages