[Fixed]-Django: Trying to make for zip work with Python Jinja

1👍

You can just use zip() in your view.

listas = zip(lista_completa, lista_completa2)
return render(request, template.html, {'listas': listas})

Then just unpack the tuple over each oteration using two variables in a for loop.

{% for pregunta, respuesta in listas %}
    # pregunta.show (or whatever your attribute for pregunta is)
    # respuesta.show
{% endfor %}

Saludos!

👤Nifled

0👍

Django does not use Jinja2 templates by default. Unless you specifically configured Django to use Jinja2, you’re using the Django template language (which has a very similar syntax).

Django’s template engine doesn’t have any nice way to do this. You just need to zip the two QuerySets before passing them into the template:

return render(request, 'todo.html', {
    'preguntas_y_respuestas': zip(lista_completa, lista_completa2)
})

In your template, you can iterate over the pairs of objects:

{% for pregunta, respuesta in preguntas_y_respuestas %}
    ...
{% endfor %}

Leave a comment