0

I have a datatable in my django app using django_tables2 and have added bootstrap pagination to it for obvious reasons using this answer. I copied the tables.html file into my templates directory and modified it as follows:

    {% if table.page %}
        {% with table.page.paginator.count as total %}
            {% with table.page.object_list|length as count %}
                {% block pagination %}
                    <ul class="pagination">
                            {% block pagination.cardinality %}
                            <li class="cardinality">
                                {% if total != count %}{% blocktrans %}Showing {{ count }} of {{ total }}
                                {% endblocktrans %}{% else %}{{ total }}{% endif %}
                                {% if total == 1 %}{{ table.data.verbose_name }}{% else %}
                                    {{ table.data.verbose_name_plural }}{% endif %}
                            </li>
                        {% endblock pagination.cardinality %}
                    </ul>
                    <div class="pull-right">
                        <nav>
                            <ul class="pagination">
                                {% block pagination.allpages %}
                                    {%  if table.page.has_previous %}
                                        <li><a href="{% querystring table.prefixed_page_field=table.page.previous_page_number %}">Previous</a></li>
                                    {% endif %}

                                    {% for p in table.paginator.page_range %}
                                        <li><a href="{% querystring table.prefixed_page_field=p %}">{{ p }}</a></li>
                                    {% endfor %}

                                    {% if table.has_next %}
                                        <li><a href="{% querystring table.prefixed_page_field=table.page.next_page_number %}">Next</a></li>
                                    {% endif %}
                                {% endblock pagination.allpages %}
                            </ul>
                        </nav>
                    </div>
                </div>
            {% endblock pagination %}
        {% endwith %}
    {% endwith %}
{% endif %}

As of now the table has very little data so I just have 2 pages. However I populated the table with random data and was able to get 10 'pages' on the table. enter image description here However this is not desirable as what if I have a 1000 entries with 10 per page, I cant have the page navigator show 100 as it would clutter up my content area. How can I go somehow limit the number of pages shown i.e. instead of 1 - 10 show up in the image above if would be something like 1|2|3|4|5|.. where clicking on the ... would show pages 6|7|8|9|10|... I tried adding previous and next buttons but not sure why they're failing.

Community
  • 1
  • 1
Beginner
  • 2,643
  • 9
  • 33
  • 51

1 Answers1

0

The easiest way is to use some pagination tag from a foreign package. You can find an extensive list of them on django-packages.

For example with django-bootstrap-pagination your pagination block should look something like

{% bootstrap_pager table.page %}
likeon
  • 791
  • 1
  • 6
  • 19