0

I have an html file in my django project as follows:

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

the content is showing for the <div class="col-sm-3">

but no content is being shown for <div class="col-sm-9">

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

One may ask why i am doing. I am just testing.

enter image description here

Edit:

I am generating the list in the following way:

def gallery(request):
    import os, sys
    img_list = os.scandir('/home/shared/pictures')
    return render(request,'blog/gallery.html', {'images': img_list})
Santhosh
  • 5,564
  • 7
  • 46
  • 107

1 Answers1

1

You might be passing a generator object instead of a list / tuple.

The difference is that you can iterate over the generator object only once.

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

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

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

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

return render(request, "template.html", {'images': list(i for i in range(10))})
Community
  • 1
  • 1
Kedar
  • 1,343
  • 8
  • 17