Django: jinja2 code not working when iterated over the same list twice
<p>I have an html file in my django project as follows:</p>

<pre><code>&lt;body&gt;
&lt;div class="row"&gt;
    &lt;div class="col-sm-3"&gt;
            &lt;ul class="list-group"&gt;
                {% for image in images %}
                &lt;li class="list-group-item"&gt;{{ image.name}}
                &lt;img class="img-responsive" src='http://krishna.com{{ image.path }}' /&gt;
                &lt;/li&gt;
                {% endfor %}
            &lt;/ul&gt;
    &lt;/div&gt;
    &lt;div class="col-sm-9"&gt;
            &lt;ul class="list-group"&gt;
                {% for image in images %}
                &lt;li class="list-group-item"&gt;{{ image.name}}
                &lt;img class="img-responsive" src='http://krishna.com{{ image.path }}' /&gt;
                &lt;/li&gt;
                {% endfor %}
            &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
</code></pre>

<p>the content is showing for the <code>&lt;div class="col-sm-3"&gt;</code></p>

<p>but no content is being shown for <code>&lt;div class="col-sm-9"&gt;</code></p>

<p>In the image below, even thought the jinja code is same for both the divs, it shows only on one side.</p>

<p>One may ask why i am doing. I am just testing.</p>

<p><a href="http://i.stack.imgur.com/UpTXz.png" rel="nofollow"><img src="http://i.stack.imgur.com/UpTXz.png" alt="enter image description here"></a></p>

<p>Edit:</p>

<p>I am generating the list in the following way:</p>

<pre><code>def gallery(request):
    import os, sys
    img_list = os.scandir('/home/shared/pictures')
    return render(request,'blog/gallery.html', {'images': img_list})
</code></pre>

<p>You might be passing a <a href="http://stackoverflow.com/a/231855/1268926">generator object</a> instead of a list / tuple.</p>

<p>The difference is that you can iterate over the generator object only once.</p>

<p>For example (below is the last / equivalent line in your view that renders the template)</p>

<pre><code>return render(request, "template.html", {'images': (i for i in range(10))})
</code></pre>

<p>Will result in <code>images</code> being iterated over only once. (In your example, the <code>col-sm-9</code> block will be empty).</p>

<p>If you need to iterate over something more than once, you must pass it as a list or tuple.</p>

<pre><code>return render(request, "template.html", {'images': list(i for i in range(10))})
</code></pre>

python django