sadaf2605 sadaf2605 - 19 days ago 4x
Python Question

Most convenient way to find last possible page of flask sqlalchemy paginate?

I am using paginate of sqlalchemy to paginate my query like following but in my front page I want to implement a next button (or a numerical page navigation links like 1,2,3,4..) and when it reaches at the last page I don't want to show user the next button (or know the maximum number of pages available in the page navigation). I would prefer not to use another database query in sqlalchemy. What is the most convenient way achieve this?

I am using following fo pagination:


One way could be checking if it returns exact number of items of my size or not what I am doing right now, if not it is the last one, but it does not satisfy corner cases where the remainder of all count and size is zero.


Check out Pagination class docs.

query = (Blog.query.filter('%'+query+'%'))
                   .paginate(page=start, per_page=size))

Then you can use query.has_prev and query.has_next properties to check if previous or next pages exist.