Paginate a list of items in python flask
Got it sorted Just need to edit
try: page = int(request.args.get('page', 1))except ValueError: page = 1List=fs.list()i=(page-1)*PER_PAGEList1=List[i:i+5]pagination = Pagination(page=page,per_page=PER_PAGE, total=len(List), search=search, record_name='List')return render_template("retrieveFile.html",List=List1,fs=fs,form="submitIt",pagination=pagination,)
Super late answer here, but hopefully this clarifies:
In addition to capturing the page and per_page values, you also need offset to help display the results on each page properly. There's actually a handy built-in to set up all three, which you can import using from flask_paginate import get_page_args
. Here's what the full view.py code would look like:
from flask_paginate import Pagination, get_page_args@search.route('/retrieve_data')def retrieve(): # get_page_arg defaults to page 1, per_page of 10 page, per_page, offset = get_page_args() # After the main query, you need to apply the per_page limit and offset fs = gridfs.GridFS(db) fs_for_render = fs.limit(per_page).offset(offset) #you can also add css_framework='bootstrap3' to Pagination for easy styling pagination = Pagination(page=page, per_page=per_page, offset=offset, total=fs.count(), record_name='List') return render_template('retrieveFile.html', fs=fs_for_render, pagination=pagination, form="submitIt")