Using zipped queryset many times in template
<p>I have a model that is only a string :</p>

<pre><code>class Data(models.Model):
    string = models.CharField(max_length=200);
</code></pre>

<p>There are <strong>2</strong> registered instances of the model in my database.</p>

<p>It is rendered by this view, which zips the queryset which another list: </p>

<pre><code>def index(request):
    data = Data.objects.all();
    data2 = [];
    for x in data:
        data2.append(0);
    return render(request, 'testApp/index.html', {"data": zip(data, data2)})
</code></pre>

<p>and here's the template code: </p>

<pre><code>{% for element, e in data %}
    {{ element.string }} {{ e }} &lt;br/&gt;
{% endforÂ %}

{% for element, e in data %}
    {{ element.string }} {{ e }} &lt;br/&gt;
{% endforÂ %}
</code></pre>

<p>This template iterates over the data twice, printing out the elements in the zipped list.</p>

<p>Here's my output:</p>

<pre><code>hello there 0 
i am a string 0 
</code></pre>

<p><strong>I am expecting 4 lines of output, because the 2 instances are looped over twice.</strong> However it's only printing them out once. What am I doing wrong?</p>

<p>In Pythno 3, <a href="https://docs.python.org/3/library/functions.html#zip" rel="nofollow"><code>zip</code></a> will give you an iterator, meaning it will be consumed on the first loop and therefore not print anything on the second loop.</p>

<p>You can fix this by casting the iterator to a list, replacing <code>zip(data, data2)</code> with <code>list(zip(data, data2))</code>.</p>

python django