Variable subtraction in django templates

DjangoDjango Templates

Django Problem Overview


It is able to write {{ myval.add:5 }}, {{ myval|add:value }} and even {{ myval|add:-5 }}.

However, I can't find out what I should type to add value * -1 like {{ myval|add:-value }}. This doesn't work, sadly.

Django Solutions


Solution 1 - Django

You need to use double quotes:

{{ myval|add:"-5" }}

This subtracts five from myval.

Solution 2 - Django

The built-in Django template tags/filters aren't all-encompassing, but it's super easy to write your own custom template tags: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

You could make your own subtract template tag pretty easily:

@register.filter
def subtract(value, arg):
    return value - arg

Solution 3 - Django

Use django-mathfilters from PyPI: https://pypi.python.org/pypi/django-mathfilters

To install :

$ pip install django-mathfilters

Then add mathfilters in your INSTALLED_APPS.

In template:

{% load mathfilters %}

<ul>
    <li>8 + 3 = {{ 8|add:3 }}</li>

    <li>13 - 17 = {{ 13|sub:17 }}</li>

    {% with answer=42 %}
    <li>42 * 0.5 = {{ answer|mul:0.5 }}</li>
    {% endwith %}

    {% with numerator=12 denominator=3 %}
    <li>12 / 3 = {{ numerator|div:denominator }}</li>
    {% endwith %}

    <li>|-13| = {{ -13|abs }}</li>
</ul>

Solution 4 - Django

I recently started working with Django and stumbled upon this one as well: I needed a very simple template loop that stops printing after n times and shows a "more" link to toggle the rest of the items.

With great interest I read the struggle of people trying to understand why this is not being added to the Django default filters (since before 2013). I didn't feel like creating a custom template tag and I kind of found a way to subtract 2 variables using strings and add in combination with with and stringformat

Let's say I have a list of items where I want to print the first 2 and hide the rest, showing how many hidden items are there, eg.

John, Anna and 5 others like this (when given a list of 7 items)

As long as the number of visible items is harcoded in the template (eg. 2), it's possible to add the negative 2 |add:"-2", but I wanted the number of visible items to be a variable as well. The Math-filter library as suggested above doesn't seem up to date (I haven't tested it with Django 2.x).

The trick seems to be to use the add helper to concat the strings "-" with the integer as string, so it can be coerced back to a negative integer in a any consecutive calls to the add helper. This doesn't work however if the value is not a string, so that's where the stringformat helper comes in.

With string value

template posts.html (note how visible is explicitely passed as string - alternative below)

{% for post in posts %}
  <h4>{{ post.title }}</h4>
  ...
  {% include 'show_likes.html' with likes=post.likes visible="3" %}
{% endfor %}

template show_likes.html (note the add:0 to make the boolean operator work)

{% with show=visible|default:"2" %}
{% for like in likes %}
  {% if forloop.counter <= show|add:0 %}
    {% if not forloop.first %},{% endif %}
    {{ like.username }}
  {% endif %}
{% endfor %}
{% if likes|length > show|add:0 %}
{% with rest="-"|add:show %}
  and {{ likes|length|add:rest }} more
{% endwith %}
{% endif %}
like this
{% endwith %}
Alternative with integer

You could just convert your integer to a string in the calling template using |stringformat:"d"

If however the number of visible items you want to show is an integer, you'll have to add a call to stringformat:"d" to have it converted to string

template posts.html

{% for post in posts %}
  <h4>{{ post.title }}</h4>
  ...
  {% include 'show_likes.html' with likes=post.likes visible=3 %}
{% endfor %}

template show_likes.html

{% with show=visible|default:2 %}
{% with show_str=show|stringformat:"d" %}
{% for like in likes %}
  {% if forloop.counter <= show %}
    {% if not forloop.first %},{% endif %}
    {{ like.username }}
  {% endif %}
{% endfor %}
{% if likes|length > show|add:0 %}
{% with rest="-"|add:show_str %}
  and {{ likes|length|add:rest }} more
{% endwith %}
{% endif %}
{% endwith %}
{% endwith %}

Since I'm a very beginner with Django and Python, I'm pretty sure this approach is far worse than actually creating a custom helper! So I'm not suggesting anyone should be using this. This was just my attempt on trying to solve this with the available template helpers and without any custom stuff.

Hope this helps

Solution 5 - Django

Lo primero es multiplicar por -1 para convertirlo en una valor negativo y guardarlo en una variable y posterior a usar la suma

> The first thing is to multiply by -1 to turn it into a negative value > and save it in a variable and then use the add

{% widthratio val2 1 -1 as result %}
{{result|add:val1}}

Solution 6 - Django

After search I found that I can make {% with var=value %} with filters to make the arithmetic operations "with other variables or not"
For example: I have x = 5 and y = 3 and need to add the y's value to x value, all what I need is these steps:
1- Create variable x : {% with x=5 %}
2- Create variable y : {% with y=3 %}
3- In my HTML tags, say <h1>, write that : <h1>{{ x|add:y }}</h1>
4- Close the y's with : {% endwith %}
5- Close the x's with : {% endwith %}
Hope it works with you, it worked with me.

{% with i=3 %}
    {% with x=1 %}
         <h1>{{i|add:x}}</h1> <!-- result is 4 -->
    {% endwith %}
{% endwith %}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaemdyView Question on Stackoverflow
Solution 1 - DjangoSimeon VisserView Answer on Stackoverflow
Solution 2 - DjangoChris ForretteView Answer on Stackoverflow
Solution 3 - DjangoMaheshView Answer on Stackoverflow
Solution 4 - Django4levelsView Answer on Stackoverflow
Solution 5 - DjangoAndres MejiaView Answer on Stackoverflow
Solution 6 - DjangoAbdelrahman SalehView Answer on Stackoverflow