templates - django for cycle add variables together -


i facing problem writing in simple cycle in template. trying write in pseudo-code.

total = 0; each dividend   total += dividend.amount;   echo total; endfor 

but can't find out, how write += line in django template file. understand should doing in views.py, somehow doing in template. code in detail.html:

{% totaldividend=0.0 %} {% dividend in stock.dividend_set.all %}         [ {{ dividend.date|date:"u000" }} , {{ totaldividend|add:dividend.amount }} ], {% endfor %} {% endwith %} 

but totaldividend|add:dividend.amount doesn't work, there zeros. how write simple cycle addition in template file? i'm doing cycle dividend.date anyway.

you write template tag set context value:

from django.template.base import library register = library()  @register.simple_tag(takes_context=true) def set_by(context, k, v):     context[k] = v     return v  {# template #}     {% totaldividend=0.0 %} {% dividend in stock.dividend_set.all %}         [ {{ dividend.date|date:"u000" }} ,       {% set_by 'totaldividend' totaldividend|add:dividend.amount %} ], {% endfor %} {% endwith %}  # or @register.assignment_tag(takes_context=true) def setby(context, k, v):     context[k] = v     return v  {% totaldividend=0.0 %} {% dividend in stock.dividend_set.all %}         [ {{ dividend.date|date:"u000" }} ,       {% set_by 'totaldividend' totaldividend|add:dividend.amount totaldividend %}{{ totaldividend }} ], {% endfor %} {% endwith %} 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -