How to add, multiply number variables in a Django template?
Asked Answered
S

6

17

The JS snippet I created relies on the forloop.counter variable being available within a {% for key, value in data.items %}..{% endfor %} tag.

Is there a way to use mathematical operators (*, -, +, /) together with the forloop.counter variable?

Sybarite answered 20/8, 2013 at 9:30 Comment(0)
S
20

There is the filter add from the documentation.

I'm pretty sure there are no built-in way to use the other mathematical operations over numbers in Django templates. You can always make your own however. It is not always a good idea to do so.

You want to keep your logic inside the views and keep the rendering inside the templates.

In your case, you should store your counter in a JavaScript variable, and use it in your snippet.

Stepha answered 20/8, 2013 at 10:22 Comment(2)
Link not working anymore. Here the latest version.Armes
One rendering application for the add filter is to add 1 to a zero based number (my example is the row number in an Excel spreadsheet) for human consumption. I guess it could be argued either way, but it's convenient for me to do it in the template rather than my Excel file parsing code or to add in in the view.Lsd
C
34

It's possible to use django built-in widthratio template tag and add filter:

  • add 5 to forloop.counter {{forloop.counter|add:5}}
  • subtract 5 from forloop.counter {{forloop.counter|add:"-5"}}
  • devide forloop.counter by 5 {% widthratio forloop.counter 5 1 %}
  • multiply forloop.counter by 5 {% widthratio forloop.counter 1 5 %}
Congresswoman answered 10/7, 2015 at 15:13 Comment(1)
Apparently, it rounds up float numbers, yes?Drus
S
20

There is the filter add from the documentation.

I'm pretty sure there are no built-in way to use the other mathematical operations over numbers in Django templates. You can always make your own however. It is not always a good idea to do so.

You want to keep your logic inside the views and keep the rendering inside the templates.

In your case, you should store your counter in a JavaScript variable, and use it in your snippet.

Stepha answered 20/8, 2013 at 10:22 Comment(2)
Link not working anymore. Here the latest version.Armes
One rendering application for the add filter is to add 1 to a zero based number (my example is the row number in an Excel spreadsheet) for human consumption. I guess it could be argued either way, but it's convenient for me to do it in the template rather than my Excel file parsing code or to add in in the view.Lsd
H
14

Use django-mathfilters. The addition filter doesn't coerce numbers to integer so you can add floats:

{% load mathfilters %}
{{ num1 | addition:num2 }}
Handel answered 12/2, 2017 at 2:2 Comment(1)
this 3rd-party solution I like the most. More flexibility, when used with JavaScript, I use floats often.Garrot
C
4

I make the next in my template file carts.html

{% extends 'base.html'%} {% block contenido %}
<h1>Checkout</h1>
<p>Subtotal + IVA: ${{ orden.sub_total }}</p>
<p>Envio:$ {{ orden.costo_envio }}</p>
<p>Total a pagar:$ {{ orden.sub_total|add:orden.costo_envio }}</p>
{% endblock %}

where my view based function is:

def carrito_checkout(request):
    if 'cart_id' in request.session:
        orden_object, created = Orden.objects.get_or_new(request)
        if orden_object is None:
            return redirect('carrito:home')
        print(orden_object)
        context = {
            "orden": orden_object
        }

    return render(request, 'carrito_checkout.html', context=context)

For me this aproach works fine

enter image description here

Cabdriver answered 8/4, 2018 at 18:35 Comment(0)
S
1

After Mathieu Marques tips, here's what I did using custom filter.

The template is rendering 5 record per page after using pagination snippet. The per page presentation reset the forloop.counter. To create a continous counter per page..

from django import template
register = template.Library()

@register.filter(name='cei')
def compute_exact_id(value, rb_page_no):    
    new_id = value+(5*(rb_page_no-1))    ## here's the mathematical operation
    return new_id

where rb_page_no is the current page number and placing {% load extra_filter %} on top my template where extra_filter is the file name where I place the compute_exact_id.

Sybarite answered 20/8, 2013 at 11:5 Comment(0)
T
0

Built-in filter reference

Adds the argument to the value. For example:

{{ value|add:"2" }}

If value is 4, then the output will be 6.

Or strings, list, etc.

{{ first|add:second }}

and first is [1, 2, 3] and second is [4, 5, 6], then the output will be [1, 2, 3, 4, 5, 6].

Transportation answered 3/6, 2024 at 11:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.