开发者

django addition

how to increment value of a variable in a template..??

{% for s in list%}
     {% for subject in s%}
             {% for sub in subject %}

                    <div id="{{ sub| getid:i }}"></div> 
                    # here i want to in开发者_StackOverflow中文版crement the value of i 
             {% endfor %}
      {% endfor %}
{% endfor %}


If you want to increase i over all nested loops, you can pass another stateful context variable, such as i=itertools.count(), and in the template, you use

<div id="{{ sub| getid:i.next }}"></div>

The Django documentation on the template language design states the philosophy of the template language is that

the template system is meant to express presentation, not program logic.

And this often means you cannot manipulate state directly with filters. To achieve state changes, you will have to create your own stateful variables whose state can be altered via a function call.


Using a template for loop? You may try this using:

forloop.counter

see the docs here: http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs

Implementation:

{% for s in list%} 
  {% for subject in s%} 
    {% for sub in subject %}
                <div id="{{ sub| getid:forloop.counter+(forloop.parentloop.counter - 1)*total_iterations_inner_loop+(forloop.parentloop.parentloop.counter-1)*total_iterations_mid_loop*total_iterations_inner_loop }}"></div> 
         {% endfor %}
  {% endfor %}
{% endfor %}


check out http://docs.djangoproject.com/en/dev/ref/templates/builtins/#add

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜