Variable subtraction in django templates

django template variables
django template split string
django custom template tags
django templates load
django-mathfilters
assignment in django template
django function in template
django template registration

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.


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

How to subtract two fields in django templates?, Unfortunately, putting a minus sign in front of a variable won't work to make it negative. You could use a custom template filter, which would be  django,django-templates. Django templates do not use inclusion so much as template inheritance. The idea is you set up a hierarchy of templates, specializing some common thing. For instance: you could have a base.html that has some basic page structure, common to all pages of your website. you could then have a


You need to use double quotes:

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

This subtracts five from myval.

Variable subtraction in django templates, Variable subtraction in django templates The built-in Django template tags/​filters aren't all-encompassing, but it's super easy to write your  How to Set a Variable in Django Templates? Posted on 2014-02-17 2019-10-22 Author vinta Posted in Python , Web Development Creating a custom template tag for assigning variables in Django template.


https://pypi.python.org/pypi/django-mathfilters

install : $ pip install django-mathfilters 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>

#361 (Some Basic Math Filters) – Django, I wanted some basic math filters (eg. add multiply divide subtract), there is already So, not having arithmetic operations in the template do exactly the opposite. I have posts that have variable parent-child levels and I need to add padding to  A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags. A template is rendered with a context. Rendering replaces variables with their values, which are looked up in the context, and executes tags.


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

Built-in template tags and filters, This is equivalent to manually applying the escape filter to each variable. The only exceptions are variables that are already marked as “safe” from escaping, either  If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template. See Template inheritance for more information. Normally the template name is relative to the template loader’s root directory.


django-variable-subtraction-in-django-templates.md, Template tags in Django are typically written as {% tag_name argument1 argument2 . For example, a variable containing a report result would actually hold <span While the HTML will render invisibly, built-in filters like add and subtract  Django makes it possible to separate python and HTML, the python goes in views and HTML goes in templates. To link the two, Django relies on the render function and the Django Template language. Request − The initial request. The path to the template − This is the path relative to the TEMPLATE_DIRS option in the project settings.py variables.


Custom Filters And Tags Reference, Django filters are designed to format template variables. The syntax to apply Django filters is the vertical bar character | also known as 'pipe' in Unix environments (  Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow paassing data from view to template, but also provides some limited features of a programming such as variables, for loops, comments, extends, if else etc. This article revolves about how to use boolean


Django Built-in Django filters, You need to load mathfilters at the top of your template. The script provides the following filters: sub – subtraction; mul – multiplication; div –  The Django template language¶ This document explains the language syntax of the Django template system. If you’re looking for a more technical perspective on how it works and how to extend it, see The Django template language: for Python programmers. Django’s template language is designed to strike a balance between power and ease.