0
{% for match in response %}
    <tr>
      <td>{{ match.i.position }}</td>
      <td>{{ match.i.team.name }}</td>
      <td>{{ match.i.games.played }}</td>
      <td>{{ match.i.games.win.total }}</td>
      <td>{{ match.i.games.lose.total }}</td>
      <td>{{ match.i.games.win.percentage }}</td>

    </tr>
    </tr>
  {% endfor %}

I am trying to increment the value of 'i' within a for loop in my standings.html django project. I am having trouble doing this as django doesnt seem to like me trying to increment values. Is there any way to fix this? I would like the value to start at 0 and increase by 1 in each iteration of the loop.

1

2 Answers 2

1

Well you should use {{forloop.counter0}} so:

{% for match in response %}
    <tr>
      <td>{{ forloop.counter0 }}</td>
      <td>{{ match.i.position }}</td>
      <td>{{ match.i.team.name }}</td>
      <td>{{ match.i.games.played }}</td>
      <td>{{ match.i.games.win.total }}</td>
      <td>{{ match.i.games.lose.total }}</td>
      <td>{{ match.i.games.win.percentage }}</td>

    </tr>
  {% endfor %}

Assuming, that you only want to show the no. of iteration of instances.

Remove i from every field in between.

Sign up to request clarification or add additional context in comments.

Comments

0

A general answer: you can't. By design . The template language is designed for displaying. Business logic should be written in Python. (Or, use Jinja instead of Django template language).

However, what you are asking is simply to access the loop iteration counter, and this is provided as part of {% for %} functionality. Inside the loop, use {{ forloop.counter0 }}.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.