Flask Rendering pagination in Jinja


Example

Here we use the object that we passed to render_template to display the pages, the current active page, and also a previous and next buttons if you can go to the previous/next page.

<!-- previous page -->
{% if users_list.has_prev %}
<li>
    <a href="{{ url_for('users', page=users_list.prev_num) }}">Previous</a>
</li>
{% endif %}

<!-- all page numbers -->
{% for page_num in users_list.iter_pages() %}
    {% if page_num %}
        {% if page_num != users_list.page %}
            <li>
                <a href="{{ url_for('users', page=page_num) }}">{{ page_num }}</a>
            </li>
        {% else %}
            <li class="active">
                <a href="#">{{ page_num }}</a>
            </li>
        {% endif %}
   {% else %}
       <li>
           <span class="ellipsis" style="white-space; nowrap; overflow: hidden; text-overflow: ellipsis">…</span>
       </li>
   {% endif %}
{% endfor %}

<!-- next page -->
{% if users_list.has_next %}
<li>
    <a href="{{ url_for('users', page=users_list.next_num) }}">Next</a></li>
{% endif %}
{% endif %}