Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
Django template forloop

Here's a excerpt from a Django template:

<div id="list-of-things">
    {% for key in things %}
        {% for thing in  thing|get_dict_value:key %}
            <div id="thing-{{ count }}">{{key}}: {{ thing }}</div>
        {% endfor %}
    {% endfor %}
</div>

things is a dict and get_dict_value:key is simply thing[key]. What's needed in the final page is:

<div id="thing-1">A: First thing</div>
<div id="thing-2">A: Second thing</div>
<div id="thing-3">B: Third thing</div>
...

...which means that I need some way of altering the count variable. Using forloop.counter here causes there to be duplicate values as the counter resets for each time through the outer loop.

There doesn't seem to be any way to set a variable in this loop, so is there another means whereby count might be incremented as required?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Here's something you can do

In the views

import itertools
counter = itertools.count()
# pass `counter` to the template

In the template

{{counter.next}} 

to display and increment the counter regardless of the loop you're in.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!