2013-09-29 104 views
-1

我在Google App Engine平臺上運行Python應用程序。布條件,我有一個術語(例如12個月),並在每個月我做一個特定的付款(付款變量)。然後我需要計算所有付款並更新TOTAL_PAYMENT變量。如何在for循環中更新外部變量(Python,Jinja2)

{% set total_payment = 0 %} 

{% for month in range(0,term) %} 

    {% payment = SOME_INT_VALUE %} 
    {% total_payment = total_payment + payment%} 

{% endfor%} 
{{ total_payment }} 

而不是總支付價值,我仍然得到0.什麼問題?

+0

我想你應該在'{%payment = SOME_INT_VALUE%}'中使用'set'標記。即'{%set payment = SOME_INT_VALUE%}'。 –

+0

@LeonYoung由於某種原因,變量重置其循環外的值 –

回答

0

也許有點髒,但它運作良好。

{% set vars = {'total_payment': 0} %} 

{% for month in range(0, term) %} 
    {% if vars.update({'total_payment': vars.total_payment + SOME_INT_VALUE}) %}{% endif %} 
{% endfor %} 

{{ vars.total_payment }}