July 2019
Beginner to intermediate
302 pages
9h 38m
English
In the Creating a basic product model recipe in Chapter 3, Data Modeling in Flask, we created a handler to list all the products in our database. If we have thousands of products, then generating a list of all of these products in one go can take a lot of time. Also, if we have to render these products on a template, then we would not want to show more than 10-20 products on a page in one go. Pagination proves to be of great help in building great applications.
Let's modify the products() method to list products to support pagination:
@catalog.route('/products') @catalog.route('/products/<int:page>') def products(page=1): products = Product.query.paginate(page, 10).items res = {} for product in products: ...