Django pagination - example from documentation. How to display with all sites number?

You can try this:

{% for num in contacts.paginator.page_range %}
  {% ifequal num contacts.number %}
    <span class="current"><b>{{ num }}</b></span>
  {% else %}
    <a href="?page={{ num }}"> {{ num }}</a>
  {% endifequal %} 
{% endfor %}

Below example is actually for class based views but would work fine. CSS is Twitter Bootstrap V3.0.0.

{% if is_paginated %}
  <ul class="pagination">
    {% if page_obj.has_previous %}
      <li><a href="?page={{ page_obj.previous_page_number }}">&laquo;</a></li>
    {% else %}
      <li class="disabled"><a href="#">&laquo;</a></li>
    {% endif %}

    {% for num in page_obj.paginator.page_range %}
      {% ifequal num page_obj.number %}
        <li class="active"><a href="#">{{ num }}<span class="sr-only">(current)</span></a></li>
      {% else %}
        <li><a href="?page={{ num }}">{{ num }}</a></li>
      {% endifequal %}
    {% endfor %}

    {% if page_obj.has_next %}
      <li><a href="?page={{ page_obj.next_page_number }}">&raquo;</a></li>
    {% else %}
      <li class="disabled"><a href="#">&raquo;</a></li>
    {% endif %}
  </ul>
{% endif %}

If someone needs Bootstap 4 Alpha 5 version of code.

Also I did two changes:

  1. I do not like duplicates of pages, so I changed in my case /profiles/?page=1 to simple /profiles/.
  2. I have lots of pages, so I do not show all pages. I show just: The 1st, the last, current and +-3 pages from the current one, and every 10th page.

If you need just all pages with Bootstrap 4 without my modifications, just remove unnecessary parts of code (everything is commented).

{% if is_paginated %}
    <nav aria-label="Page navigation">
        <ul class="pagination">
            <!-- << PREVIOUS PART -->
            <!-- << Disable 'Previous' page button if you are at the 1st page -->
            {% if not page_obj.has_previous %}
                <li class="page-item disabled">
                    <a class="page-link" href="#" tabindex="-1" aria-label="Previous">

            <!-- << If you are at the 2nd page,
            'Previous' page button will get '/profiles/' url instead of '/profiles/?page=1' -->
            {% elif page_obj.previous_page_number == 1 %}
                <li class="page-item">
                    <a class="page-link" href="{{ profiles_1st_page_url }}" aria-label="Previous">

            {% else %}
                <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}" aria-label="Previous">
            {% endif %}

                        <span aria-hidden="true">&laquo;</span>
                        <span class="sr-only">Previous</span>
                    </a>
                </li>

            <!-- PAGES PART -->
            {% for num in page_obj.paginator.page_range %}
                <!-- Active page -->
                {% if num == page_obj.number %}
                    <li class="page-item active">
                        <a class="page-link" href="#">{{ num }}<span class="sr-only">(current)</span></a>
                    </li>

                {% else %}
                    <li class="page-item">
                        <!-- For the 1st page we do not use ?page=1 -->
                        {% if num == 1 %}
                            <a class="page-link" href="{{ profiles_1st_page_url }}">{{ num }}</a>

                        {% else %}
                            <!-- Show current page and +-3 pages -->
                            {% if num|add:"-3" <= page_obj.number and page_obj.number <= num|add:"3" %}
                                <a class="page-link" href="?page={{ num }}">{{ num }}</a>

                            <!-- Shows every 10th page and the last page -->
                            {% elif num|divisibleby:"10" or num == page_obj.paginator.num_pages %}
                                <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                            {% endif %}

                        {% endif %}
                    </li>
                {% endif %}

            {% endfor %}

            <!-- >> NEXT PART -->
            {% if not page_obj.has_next %}
                <!-- << Disable 'Next' page button if you are at the last page -->
                <li class="page-item disabled">
                    <a class="page-link" href="#" tabindex="-1" aria-label="Next">

            {% else %}
                <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.next_page_number }}" aria-label="Next">
            {% endif %}

                        <span aria-hidden="true">&raquo;</span>
                        <span class="sr-only">Next</span>
                    </a>
                </li>

        </ul>
    </nav>
{% endif %}

And it looks like: enter image description here

Tags:

Django