Seb Blackley
Seb Blackley

Reputation: 18

Flask paginate "per page" not changing the amount of visible items

I am following a tutorial to implement flask paginate into my project. The default per_page value is equal to 10. However I want to change this to something else (e.g 5). But when I change the value from 5 to 10, nothing happens. Is there something else that needs to be changed ?

App.py

def get_coins(offset=0, per_page=5):
    return DataPull.coin_output[offset: offset + per_page]


@app.route('/')
def index():
    page, per_page, offset = get_page_args(page_parameter='page',per_page_parameter='per_page')
    total = len(DataPull.coin_output)
    pagination_coins = get_coins(offset=offset, per_page=per_page)
    pagination = Pagination(page=page, per_page=per_page, total=total,css_framework='bootstrap4')
    return render_template('index.html', table_data=pagination_coins,page=page,per_page=per_page,pagination=pagination,)

if __name__ == '__main__':
    app.run(debug=True)

Front-end:

<tbody>
    {% for i in table_data %}
        <tr class="clickable-row" data-href="/{{ i.id | lower }}">
            <td>{{ loop.index + (page - 1) * per_page }}</td>
            <td><img src="{{ i.logo_url }}" style="width: 30px; height: 30px;">{{ i.id }}</td>
            <td>{{ i.name }}</td>
        </tr>
    {% endfor %}
</tbody>

Upvotes: 0

Views: 1267

Answers (1)

Jyoti
Jyoti

Reputation: 26

Not sure if you are still looking for any answer. Quick solution, try replacing the following line

page, per_page, offset=get_page_args(page_parameter='page',per_page_parameter='per_page')

with

page = int(request.args.get('page', 1))

per_page = 5

offset = (page - 1) * per_page

You will get 5 records per page.

Enjoy.

Upvotes: 1

Related Questions