Counter within django loop
<p>I have the following django / html code:</p>

<pre><code>{% for x in fixtures %}

    &lt;div id="match1" style="display:block"&gt;{{x.home_team.teamname}}&lt;/div&gt;

{% endfor %}
</code></pre>

<p>The above simply displays all teams from a set of fixtures.  It works perfectly as it stands.  However, I would like the div id to go up 1 each time it runs through the loop.  I.e "match1" then "match2" then "match3" etc.</p>

<p>I am assuming javascript is my friend here? but some direction would be appreciated.</p>

<p>Many thanks in advance for any assistance :)</p>

<p>There's no need for javascript. You can use the <code>forloop.counter</code> variable:</p>

<pre><code>{% for x in fixtures %}

    &lt;div id="match{{ forloop.counter }}" style="display:block"&gt;{{x.home_team.teamname}}&lt;/div&gt;

{% endfor %}
</code></pre>

<p>The various variables available in a for loop are described in <a href="https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#for" rel="nofollow">the docs</a>. </p>

javascript python django