Flask-Bootstrap render_pagination() not changing to active page

1.6k views Asked by At

I am using Flask-Bootstrap's render_pagination macro. When I click the page numbers, it changes the URL to have page=2, but when the page re-renders, the results and page are still for page 1. How do I change the page and results when using render_pagination?

@app.route('/results')
def results(filename, page=1):
    logs = Logs.query.paginate(page)
    return render_template('results.html', logs=logs)
{% from "bootstrap/pagination.html" import render_pagination %}

{% for log in logs.items %}
    {{ log }}
{% endfor %}

{{ render_pagination(logs) }}
1

There are 1 answers

0
davidism On

Flask-Bootstrap only deals with the frontend, you need to implement pagination on the backend. It renders links to the current view while changing the page query arg. You need to access this arg from request.args in the view and use it when paginating your query, by passing it to query.paginate.

from flask import request

@app.route('/results')
def results():
    page = request.args.get('page', 1)
    query_results = Episodes.query.paginate(page=page)
    return render_template('results.html', query_results=query_results)