Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda