1

I created a practice blog using Flask and Peewee. I wanted to include pagination in the app . I followed Colifier example blog pagination and it works fine. My question, is there a way for me show number of pages like this:

pages pagination

This is the example pagination that I followed:

example pagination

This is what the pagination code looks like:

pagination.html

{% if pagination.get_page_count() > 1 %}
<ul class="pager">
    {% if pagination.get_page() > 1 %}
       <li class="previous"><a href="./?{{ request.args|clean_querystring('page', page=pagination.get_page() - 1) }}">&laquo; Previous {{ pagination.get_page() - 1 }} / {{ pagination.get_page_count() }}</a></li>
    {% else %}
       <li class="previous disabled"><a href="#">&laquo; Previous</a></li>
    {% endif %}
    {% if pagination.get_page_count() > pagination.get_page() %}
       <li class="next"><a href="./?{{ request.args|clean_querystring('page', page=pagination.get_page() + 1) }}">Next {{ pagination.get_page() + 1 }} / {{ pagination.get_page_count() }} &raquo;</a></li>
    {% else %}
       <li class="next disabled"><a href="#">Next &raquo;</a></li>
    {% endif %}
</ul>
{% endif %}

init.py

@app.template_filter('clean_querystring')
def clean_querystring(request_args, *keys_to_remove, **new_values):
querystring = dict((key, value) for key, value in request_args.items())
for key in keys_to_remove:
    querystring.pop(key, None)
querystring.update(new_values)
return urllib.parse.urlencode(querystring)

Edit: I added this in the pagination template but I don't think this looks right

# Pagination numbers
{% for page_num in page %}
    {% if page_num %}
        {% if page.pages == page_num %}
            <a href="{{ request.args|clean_querystring('page', page=pagination.get_page()) }}" class="btn btn-primary">{{page_num}}</a>
            {% else %}
            <a href="{{ request.args|clean_querystring('page', page=pagination.get_page()) }}" class="btn btn-outline-primary">{{page_num}}</a>
        {% endif %}
    {% endif %}
{% endfor %}
Ismail9403
  • 59
  • 7
  • 1
    You have everything you need -- the pagination gives you the number of pages and the current page. You will just need to sort out iterating and eliding with some additional conditionals / loop. – coleifer Sep 30 '21 at 12:35
  • All within the html template or in the route file? – Ismail9403 Feb 04 '22 at 03:31

1 Answers1

0

I finally found a way to do it if anyone needs an answer:

{% for page_num in range(1, pagination.get_page_count() + 1) %}
    {% if pagination.get_page() == page_num %}
        <a href="./?{{ request.args|clean_querystring('page', page=page_num) }}" class="btn btn-primary">{{ page_num }}</a>
    {% else %}
        <a href="./?{{ request.args|clean_querystring('page', page=page_num) }}" class="btn btn-outline-primary">{{ page_num }}</a>
    {% endif %}
{% endfor %}

Now I need to know how to iterate the pages threshold if there's more than 30 pages because I don't want display all 30 pages. If anyone have suggestions, let me know:

Pagination threshold

Ismail9403
  • 59
  • 7