在 Django 中减去标签

Subtract tags in Django

我想知道是否可以在 Django 中减去两个标签变量,像这样,感谢您的帮助,下面我提供了我已经使用过的代码,有没有专家知道这个问题?

{% for user_information in values.qs %}
<td>({{user_information.amount|floatformat:2|intcomma}} - {{user_information.amount_paid|floatformat:2|intcomma}}) </td>
{% endfor %} 

Updated using math filters

    {% load mathfilters %}
    {% for user_information in benefeciaries.qs %}
    {% with amount1=user_information.amount amount2=user_information.amount_paid %}    
    <td>answer= {{ amount1|sub:amount2 }}</td> 
   {% endwith %}                
   {% endfor %}

这个 post 有一些很好的答案,您可以使用自定义模板标签或使用数学过滤器。

要使用数学过滤器: 1.

$ pip install django-mathfilters
  1. 在您的 settings.py 中将 mathfilters 添加到 INSTALLED_APPS
  2. 在您的模板中:
{% load mathfilters %}
 
    {% with amount1=user_information.amount amount2= 
    user_information.amount_paid %}    

   <li>answer= {{ amount1|sub:amount2 }}</li> 
   {% endwith %}                 

更新 2 在 op 显示完整冗长的 html 模板之后。

问题是你写了两次{for ...}。在你的 html 文件的开头,你已经有 {for...} 所以使用 {% load mathfilters %} 你不需要再次添加它。