I am working on pagination in flask(Python framework) using flask-paginate (just for ref)
I am able to achieve pagination for just a find
query as below:
from flask_paginate import Pagination
from flask_paginate import get_page_args
def starting_with_letter(letter):
page, per_page, offset = get_page_args()
collection_name=letter.lower()+'_collection'
words=db[collection_name]
data_db=words.find()
data=data_db.limit(per_page).skip(offset) '''Here I have achieved the limit and skip'''
pagination = Pagination(page=page, total=data.count(),per_page=per_page,offset=offset,record_name='words')
return render_template('startingwords.html',data=data,pagination=pagination)
But I am not able to do the same for the aggregate here:
def test():
page, per_page, offset = get_page_args()
cursor_list=[] '''appending each cursor in iteration of for loop '''
collections=db.collection_names()
for collection in collections:
cursor_objects = db[collection].aggregate([
{
"$match": {
"$expr": {"$eq": [{"$strLenCP": "$word"}, 6]}
}
},
{"$skip": offset},
{"$limit": per_page}
])
for cursor in cursor_objects:
cursor_list.append(cursor)
pagination = Pagination(page=page, total=len(cursor_list),per_page=per_page,offset=offset,record_name='words')
return render_template('lettersearch.html',data=cursor_list,pagination=pagination)
The results are displayed as :
Here all the 39
results are shown at single page
On hitting page 2
it showed :
Note: By default flask-paginate sets initially per_page
as 10
and offset
as 0
after referring many links i have tried:
placing skip
and limit
above match
which is wrong any way
Also learnt that limit
is always followed by skip
I am stuck with this, Any help is appreciated