[Fixed]-How to do pagination using mongoengine?

30👍

You can use skip and limit from QuerySet to achieve pagination.
For example if you want to show the second page with a limitation of 10 items per page, you can do like this:

page_nb = 2 
items_per_page = 10 

offset = (page_nb - 1) * items_per_page

list = Books.objects.skip( offset ).limit( items_per_page )
👤Eric

2👍

The flask-mongoengine plugin has an example of a paginator you could adapt to follow the digg paginator.

👤Ross

1👍

You can use array-slicing syntax as well which is a bit more decent and readable :

begin = (page - 1) * page_size # offset
end = offset + page_size
list = Books.objects[begin:end]()

Leave a comment