title: Django Template: How to use parent variable in nested for loop
body: <pre><code>{% for round in rounds %}
    &lt;div id="round_{{forloop.counter}}"&gt;
      {% for match in r{{forloop.counter}}_matches %}
          &lt;p&gt;I am match {{ match }}!&lt;/p&gt;
          &lt;p&gt;I am one of the matches in round {{round}}!&lt;/p&gt;
      {% endfor %}
    &lt;/div&gt;
  {% endfor %}
  </code></pre>
  
  <p><code>rounds</code> and <code>r1_matches, r2_matches</code> etc. are lists of objects defined in the view that leads to this template.</p>
  
  <p>Obviously, this snippet is showing logically what I want to happen, but it doesn't work for obvious reasons.  In lieu of being able to use <code>{{forloop.counter}}</code> inside the second <code>for</code> loop, how can I go about only loading up matches that are a part of the current round?</p>
  
  <p>As a side note, I also have a variable being passed to this template which isn't shown here, called <code>matches</code>, and each <code>match</code> object has a field called <code>round</code> which tells you which round that match belongs in.  I figured I would do more of the logic in the view, though, and so I have <code>r1_matches, r2_matches</code> etc. pre-snipped out into their own lists.  Point being, if it makes more sense to do something like <code>{% for match|"round={{forloop.counter}}" in matches %}</code> I would gladly do it, but then I STILL need access to the parent loop's counter same as the other way.</p>