Find last possible page of Plask-SQLAlchemy paginate
<p>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? </p>

<p>I am using following fo pagination:</p>

<pre><code>Blog.query.filter(Blog.title.like("%"+query+"%")).paginate(page=start,per_page=size).items
</code></pre>

<p>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. </p>

<p>Check out <a href="http://flask-sqlalchemy.pocoo.org/2.1/api/?highlight=paginate#flask.ext.sqlalchemy.Pagination" rel="nofollow"><code>Pagination</code></a> class docs.</p>

<pre><code>query = (Blog.query.filter(Blog.title.like('%'+query+'%'))
                   .paginate(page=start, per_page=size))
</code></pre>

<p>Then you can use <code>query.has_prev</code> and <code>query.has_next</code> properties to check if previous or next pages exist.</p>

python flask sqlalchemy flask-sqlalchemy