Reputation: 580
I am trying to use one loop in my HTML File using Jinja template First loop is for showing data on slider Second is for list tag The problem is that both are not showing data simultaneously I have to remove one loop to make other loop working
For Slider
<ul class="rslides" id="slider">
{% for post in slider %}
<li>
<img src="{{post.Image}}" alt="">
<div class="caption">
<a href="#">{{post.Heading}}</a>
</div>
</li>
{% endfor %}
for List Tag
{% for post in posts %}
<div class="article">
<div class="article-left">
<a href="#"><img src="{{post.Image}}" alt="" /></a>
</div>
<div class="article-right">
<div class="article-title">
<p style="color:black">{{ post.Date }}<a class="span_link" href="#"><!-- <span class="glyphicon glyphicon-comment"></span>0 </a><a class="span_link" href="#"><span class="glyphicon glyphicon-eye-open"></span></a><a class="span_link" href="#"><span class="glyphicon glyphicon-thumbs-up"></span>89</a> --></p>
<a class="title" href="{{url_for('post',post_id=post._id)}}">{{ post.Heading }}</a>
</div>
<div class="article-text">
<p>{{ post.NewsType }}...</p>
<!-- <a href="single.html"><img src="{{ url_for('static', filename='images/more.png')}}" alt="" /></a> -->
<div class="clearfix"></div>
</div>
</div>
<div class="clearfix"></div>
</div>
{% endfor %}
Flask Code
allpost = posts.find( {"NewsType": {"$in": it}}).sort('Date',pymongo.DESCENDING).skip((page - 1) * per_page).limit(per_page)
pagination = Pagination(page=page,per_page=5,total=allpost.count(), search=search, record_name='allpost')
return render_template('index.html', posts=allpost,pagination=pagination,slider=allpost)
Both loops are getting values from mongodb collection . Is there anyway to solve this problem?
Upvotes: 0
Views: 1093
Reputation: 24966
A MongoDB query hands back an object that fetches results lazily. And once those results are fetched, that's it.
Your code is trying to consume allposts
twice. What you're seeing is that the first loops through works, leaving nothing for the second loop.
One way to fix that is to consume the results once, turning them in to a list, before passing the list to your template. That is, add
allpost = list(allpost)
before passing that to the template.
Upvotes: 1