在python
我可以很容易地定义和重新定义一个变量。
例如,(设变量为sum
):
>>> sum=0
>>> y=[1,2,3,4]
>>> for n in y:sum+=n
>>> sum
10
有可能做同样的事情django templates
吗?
我试过了:
{% with sum=0 %}
{% for emp in employees %}
<!--i dont want to print, but just redefine the variable sum -->
{{ sum|add:emp.credit }}
.........
{% endfor %}
<!-- now i want to print the sum. it is easy-->
{{ sum }}
{% end with %}