views:

39

answers:

1

I want to a do for loop like (for int x = 0; x < 3; x++) in a django template. How should I do it? Pseudo code looks like the following:

{% for Summary in Summary_list %}
        {% ifchanged Summary.bu_id %}
            </tr>
            <tr>
            <td>{{Summary.bu.version}}</td>
            {% if Summary.platform_id != 1 %}
                {% for x less than Summary.platform_id %}
                        <td><center>-</center></td>
                        {% x++ %}

{# How Should I do this part? #}

            <td> <center>{{Summary.successCount}}</center></td>
        {% else %}
            <td><center> {{Summary.successCount}}</center></td>
        {% endifchanged %}
    {% endfor %}

Thank you very much!

A: 

Use the template range filter in this snippet. (For background on using custom filters consult the documentation.)

Then you should be able to do something like:

{% for x in Summary.platform_id|get_range %}

...

{% endfor %}
ars
Thank you so much! I have one more question. I need to declare a variable to store a number retrieved from the template. I know that I cannot declare a variable in the template but in the view. How should I declare it in the views? And how can I update the value when {% for Summary in Summary_list %} updates??Thank you!!
Jimmy
There's no good way to do this, since logic is for views and templates are only for presentation. It seems though that any information available to a template is available to a view. For example, you have access to Summary_list in the view. So you should be able to do what you need without retrieving a parameter from the template.
ars