I am having trouble implementing pagination with Flask-SQLAlchemy or Flask-Pagination, either or. I am unsure how to initialize the pagination, setting pages, determining pages, offest, etc. I am coming from PHP, quite new to Python.
I am querying all the posts in my database
posts = Posts.query.order_by(Posts.time.desc()).all()
I have been looking at the following examples:
- http://www.ergo.io/tutorials/better-pagination-in-flask/better-pagination-in-flask/
- https://pythonhosted.org/Flask-paginate/
- sqlalchemy pagination
I am confused on really what to do, the information I am finding greatly differs between articles. It's left me with confusion and not knowing where to begin. I want to query all rows of the database table, limit the results to 20 and paginate. I'm not seeing this clearly.
Controller
in the View
I recommend using Flask-SQLAlchemy's pagination: http://flask-sqlalchemy.pocoo.org/2.1/api/?highlight=pagination#flask.ext.sqlalchemy.Pagination
There's a well-written example here: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-ix-pagination
Here's the basic idea for the view:
And then for the template (I don't know your posts model so I made something up):