0
{% for round in rounds %}
  <div id="round_{{forloop.counter}}">
    {% for match in r{{forloop.counter}}_matches %}
        <p>I am match {{ match }}!</p>
        <p>I am one of the matches in round {{round}}!</p>
    {% endfor %}
  </div>
{% endfor %}

rounds and r1_matches, r2_matches etc. are lists of objects defined in the view that leads to this template.

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 {{forloop.counter}} inside the second for loop, how can I go about only loading up matches that are a part of the current round?

As a side note, I also have a variable being passed to this template which isn't shown here, called matches, and each match object has a field called round 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 r1_matches, r2_matches etc. pre-snipped out into their own lists. Point being, if it makes more sense to do something like {% for match|"round={{forloop.counter}}" in matches %} I would gladly do it, but then I STILL need access to the parent loop's counter same as the other way.

1 Answer 1

1

really you should do

context = {"rounds":[["match1","match2","match3"],["match2","match1"]]

{% for round in rounds %}
   <div id="round_{{forloop.counter0}}">
      {% for match in round %}
          I am match {{forloop.counter1}}
          in round {{forloop.counter0 }}
      {% endfor %}
   </div>
{% endfor %}

I think thats right at least ...

iirc counter0 is the outermost forloop ,counter1 is the next inner (I think you can go as far as 9)

counter refers to the current one

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

1 Comment

Ah! I've been looking at it too long. Just one more packaging step in the view and all is well. Thanks friend!

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.