Two For loops in one HTML page using Jinja (Not working) - mongodb

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">
{{post.Heading}}
</div>
</li>
{% endfor %}
for List Tag
{% for post in posts %}
<div class="article">
<div class="article-left">
<img src="{{post.Image}}" alt="" />
</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>
<!-- <img src="{{ url_for('static', filename='images/more.png')}}" alt="" /> -->
<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?

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.

Related

NoReverseMatch Error, Django can't verify the edit page

$I have no solution for this error , please i need support, it gives noreversematch and it seems it cant reach my edit-page , the following if full details of the coding , thank you
$ Edit link to edit page:
{% if post.created_by == user %}
<div class="mt-3">
<a class="btn btn-success float-end"
href="{% url 'edit_post' post.topic.board.pk post.topic.pk post.pk %}">Edit</a>
</div>
{% endif %}
$ views.py
#method_decorator(login_required, name='dispatch')
class PostUpdateView(UpdateView):
model = Post
fields = ['message']
template_name = 'edit_post.html'
pk_url_kwarg = 'post_id'
context_object_name = 'post'
def form_valid(self, form):
post = form.save(commit=False)
post.updated_by = self.request.user
post.updated_date = timezone.now()
post.save()
return redirect('topic_posts', board_id=post.topic.board.pk, topic_id=post.topic.pk)
$ urls.py
path('<int:board_id>/topics/<int:topic_id>/posts/<int:post_id/edit',
views.PostUpdateView.as_view(), name="edit_post"),
$html of the edit-page
<div class="container container-newtopic w-50">
<h1>Edit Post</h1>
<br>
{% include 'parts/alerts.html' %}
<div aria-label="breadcrumb">
<ol class="breadcrumb n1-head">
<li class="breadcrumb-item">Boards</li>
<li class="breadcrumb-item"><a href="{% url 'topics' post.topic.board.pk
%}">
{{post.topic.board.name}}</a></li>
<li class="breadcrumb-item active" aria-current="page"><a href="">Edit
Post</a></li>
</ol>
</div>
<form method="POST" action="" novalidate>
{% csrf_token %}
<input type="hidden" name="next" value="{{next}}">
{% include 'includes/form.html' %}
<button type="submit" name="btnaddtopicdj"
class="btn main-btn w-100 rounded-pill mt-5">Save Changes
</button>
</form>
</div>
$ error message:
NoReverseMatch at /boards/3/topics/20
Reverse for 'edit_post' with arguments '(3, 20, 23)' not found. 1 pattern(s) tried:
['boards/(?P<board_id>[0-9]+)/topics/(?P<topic_id>[0-9]+)/posts/<int:post_id/edit\\Z']
Reverse for 'edit_post' with arguments '(3, 20, 23)' not found. 1 pattern(s) tried:
['boards/(?P<board_id>[0-9]+)/topics/(?P<topic_id>[0-9]+)/posts/<int:post_id/edit\\Z']
href="{% url 'edit_post' post.topic.board.pk post.topic.pk post.pk %}">Edit</a>

Map through an object in scala play framework map and include a file

I am using scala play framework.
On my scala.html page I am trying to loop through a list using a map function
In each loop I want to call an included page.
{profilesvm.profiles.map(profile=>{
<div class="col-lg-3 col-md-4 col-sm-6 col-xs-12">
#cards(profile)
</div>
}
Right now the #cards is coming out printed as straight text. instead of pulling the code.
Do I need to put something around the words #cards to make it run as scala?
Just had to adjust brackets.
<div class="col-md-10 col-sm-8 col-xs-12 ">
#featuredProfiles.map{profiles=>
<div id="featured-profiles" >
<div class="list col-xs-12">
#for(p <- profiles.profiles){
<div class="col-lg-3 col-md-4 col-sm-6 col-xs-12 list-inside">
#cards(p){
}
</div>
}
}
</div>
</div>

Using more than one ajax-form in one ejs Sails.js

I am trying to add two forms in my ejs page using ajax-form tags and sails.js 1.0.0. Can you have multiple tags inside one ejs page? In the first form the action is updateMaplayers which looks at the controller update-maplayers.js file to update any records and the maplayers action on the second ajax-form looks at the maplayers.js controller to add a new record. When I try to run either actions they both fail if I have both . If I remove one of the ajax-form actions I I can submit. Does anyone have any examples?
My ejs file looks like
<div id="maplayers-main" v-cloak>
<div class="container" >
<div class="row">
<div class="col-sm-6">
<h1>Map Layers</h1>
</div>
<div class="col-sm-6">
<button type="button" class="btn btn-outline-primary float-right" #click="openAddMapLayerForm()">Add</button>
</div>
</div>
<hr/>
<div id="maplayers-container" >
<% _.each(maplayers, function (maplayer) { %>
<ajax-form action="updateMaplayers" :syncing.sync="syncing" :cloud-error.sync="cloudError" #submitted="submittedForm()" :handle-parsing="handleParsingForm">
......
</ajax-form>
<% }) %>
</div>
<div id="maplayers-add-container">
<ajax-form action="maplayers" :syncing.sync="syncing" :cloud-error.sync="cloudError" #submitted="submittedForm()" :handle-parsing="handleParsingForm">
........
</ajax-form>
</div>
</div>
</div>
<%- /* Expose locals as `window.SAILS_LOCALS` :: */ exposeLocalsToBrowser() %>

Selenium- Tag traversal Xpath for facebook First name not working

<div id="reg_form_box" class="large_form">
<div class="clearfix _58mh">
<div class="mbm _3-90 lfloat _ohe">
<div id="u_0_0" class="_5dbb">
<div class="uiStickyPlaceholderInput uiStickyPlaceholderEmptyInput">
<div class="placeholder" aria-hidden="true">First name</div>
<input id="u_0_1" class="inputtext _58mg _5dba _2ph-" data-type="text" name="firstname" aria-required="1" placeholder="" aria-label="First name" aria-controls="js_0" aria-haspopup="true" role="null" aria-describedby="js_w" aria-invalid="true" type="text"/>
</div>
<i class="_5dbc img sp_beZQzZ7Rg6Q sx_5ca7f2"/>
<i class="_5dbd img sp_beZQzZ7Rg6Q sx_9c246c"/>
</div>
Above is the code for which i want to write Xpath using tag name traversal. Here is the xpath i have made
"//div[#id='reg_form_box']/div[1]/div[1]/div[1]/div/input"
Please suggest what's wrong here and how can i correct the same. Website is Facebook and field is First name on homepage.
Ideally unless you have a case of multiple modes that match the same xpath, you don't have to traverse through the entire hierarchy.
This will work:
//input[#name='firstname']

How to get the index of the ACTIVE CHILD div of my container with jQuery?

<div id="container">
<div class="active">
<div class="active"></div>
</div>
<div>
</div>
</div>
How to write such a container?
Use .index() without parameters to get it's (0-based) index amongst siblings, like this:
var index = $("#container .active").index();
You can test it out here. For your example markup, it would be 0.