Tayyab
Tayyab

Reputation: 109

how to reassign value to a variable after doing arithmetic operations in Django Template Language

I want to do arithmetic operations within Django Template format and also assign the new value to the same Variable

I have so far used {{with}} {{set}} and also django-mathfilters but i am unable to reassign value back to the same variable.

                    {% with count=0 %}
                          {% for report in reports %}
                            <tr>
                                <th scope="row">{{set count count|add:"1"}}</th>
                                <td>{{report.submit_time}}</td>
                                <td>{{report.file_name}}</td>
                                {% if report.is_complete %}
                                    <td>Complete</td>
                                {% else %}
                                    <td>Pending</td>
                                {% endif %}
                                <td>{{report.score}}</td>
                            </tr>
                            {% endfor %}
                        {%endwith%}

i want to show numbers in serial in my table

Upvotes: 0

Views: 166

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600059

There is no need to do any calculations here. Django provides you with a counter in every for loop.

<th scope="row">{{ forloop.counter }}</th>

(Note, you are using Django template language here, not Jinja2.)

Upvotes: 1

Related Questions