2
class Shipment(ListView):
    template_name = "listing-base.html"
    model = Shipment
    context_object_name = "shipment_list"
    paginate_by = 25

The Output Comes like this

Previous Page 1   of  7  NEXT PAGE

Instead i need

 Previous Page 1 2 ...5..7  NEXT PAGE

Please help Thanks in advance "

falsetru
  • 357,413
  • 63
  • 732
  • 636
Harish TM
  • 39
  • 6

3 Answers3

5

This one was tested with CBV and it's a modified version of the code from this blog post.

Pagination is added in a template tag, so you can just load it for any of your templates rendered with a ListView, for example, for listing-base.html:

{% load paginator_tags %}

<!-- your list -->

{% get_pagination 2 1 %}

get_pagination is a template tag defined in paginator_tags.py. Changing first_last_amount and before_after_amount you can control how many pages you want to show:

Previous  1  2  ... 5  6  7  8  9  10  11  ... 25  26  Next

paginator_tags.py:

from django import template

register = template.Library()


@register.inclusion_tag('_pagination.html', takes_context=True)
def get_pagination(context, first_last_amount=2, before_after_amount=4):
    page_obj = context['page_obj']
    paginator = context['paginator']
    is_paginated = context['is_paginated']
    page_numbers = []

    # Pages before current page
    if page_obj.number > first_last_amount + before_after_amount:
        for i in range(1, first_last_amount + 1):
            page_numbers.append(i)

        if first_last_amount + before_after_amount + 1 != paginator.num_pages:
            page_numbers.append(None)

        for i in range(page_obj.number - before_after_amount, page_obj.number):
            page_numbers.append(i)

    else:
        for i in range(1, page_obj.number):
            page_numbers.append(i)

    # Current page and pages after current page
    if page_obj.number + first_last_amount + before_after_amount < paginator.num_pages:
        for i in range(page_obj.number, page_obj.number + before_after_amount + 1):
            page_numbers.append(i)

        page_numbers.append(None)

        for i in range(paginator.num_pages - first_last_amount + 1, paginator.num_pages + 1):
            page_numbers.append(i)

    else:
        for i in range(page_obj.number, paginator.num_pages + 1):
            page_numbers.append(i)

    return {
        'paginator': paginator,
        'page_obj': page_obj,
        'page_numbers': page_numbers,
        'is_paginated': is_paginated,
    }

_pagination.html file:

{% if is_paginated %}
<div class="pagination-wrapper">
    <ul class="pager">
    {% if page_obj.has_previous %}
        <li><a href="?page={{ page_obj.previous_page_number }}">Previous</a></li>
    {% endif %}
    {% for page in page_numbers %}
        {% if page %}
            {% ifequal page page_obj.number %}
                <li class="disabled"><a href="#">{{ page }}</a></li>
            {% else %}
                <li><a href="?page={{ page }}">{{ page }}</a></li>
            {% endifequal %}
        {% else %}
            ...
        {% endif %}
    {% endfor %}
    {% if page_obj.has_next %}
        <li><a href="?page={{ page_obj.next_page_number }}">Next</a></li>
    {% endif %}
    </ul>
</div>
{% endif %}
cansadadeserfeliz
  • 3,033
  • 5
  • 34
  • 50
2

You can use Django Endless Pagination and easily implement digg-styled pagination with it.

amureki
  • 773
  • 1
  • 8
  • 15
  • But i am not getting how yo use it in Class Based Views – Harish TM Jul 31 '15 at 06:48
  • @HarishTM It's almost same like ListView, but you should use AjaxListView (from endless_pagination.views). Check out example of usage at this question: http://stackoverflow.com/questions/18014870/django-endless-with-class-based-views-example – amureki Jul 31 '15 at 06:59
0

Try follwing approch

in views.py

page = request.POST['page_numb']
paginator = Paginator(object_list, 10)
lenth_data1 = paginator.num_pages
lenth_data = range(1, lenth_data1 + 1)
return render(request, 'home/home.html', {'lenth_data':lenth_data})

in html

<input type="hidden" value="{{ organization_search.number }}" id="page_numb2"name="page_numb">
<ul class="pagination">
                                {% if organization_search.has_previous %}
                                    <li class="waves-effect overlay">
                                        <a
                                                id="{{ i }}nexssa"
                                                onclick="nex({{ organization_search.previous_page_number }})">
                                            <i class="mdi-navigation-chevron-left"></i></a></li>
                                {% else %}
                                    <li class="disabled"><a href="#!"><i class="mdi-navigation-chevron-left"></i></a>
                                    </li>
                                {% endif %}
                                {% for i in lenth_data %}
                                    <li class="overlay waves-effect {% if forloop.counter == organization_search.number %}active{% endif %}">
                                        <a
                                                id="{{ i }}nex" onclick="nex({{ i }})">{{ i }}</a></li>
                                {% endfor %}
                                {% if organization_search.has_next %}
                                    <li class="waves-effect">
                                        <a
                                                id="{{ i }}nexsa"
                                                onclick="nex({{ organization_search.next_page_number }})"><i
                                                class="mdi-navigation-chevron-right"></i></a></li>

                                    {#                                    <a href="?page={{ organization_search.next_page_number }}"><i#}
                                    {#                                        class="mdi-navigation-chevron-right"></i></a></li>#}
                                {% else %}
                                    <li class="disabled"><a href="#!"><i class="mdi-navigation-chevron-right"></i></a>
                                    </li>
                                {% endif %}
                            </ul>

                        </div>
                    </div>
function nex(i) {

                            $("#page_numb2").val(i);
                            $("#first_form").submit();
                        }

hope this will help

Wagh
  • 4,202
  • 5
  • 39
  • 62