Custom Pagination for Dynamic Usable. Good night everybody, this time i wanna show simply how to custom pagination for dinamic usability. This usefull if you want display your number of pagination and work with much pagination egg: generic.ListView or other pagination in your

Thereis script:

from django.core.paginator import (Paginator, EmptyPage, PageNotAnInteger)

class Custom_Pagination(object):
    """Custom dinamic paginations for all views bassed in `generic.ListView` or else
    egg: Custom_Pagination(self.get_queryset(), self.paginate_by, self.request.GET.get('page')).pagination()
    def __init__(self, queryset, numb_pages, request_page):
        self.queryset         = queryset             #egg: models.Video.objects.published()
        self.numb_pages     = numb_pages         #egg: int 6 `is number per page`. or from param: `paginate_by` in `generic.ListView`.
        self.request_page     = request_page        #egg: self.request.GET.get('page')

    def pagination(self):
        paginator     = Paginator(self.queryset, self.numb_pages)
        page         = self.request_page
            all_videos =
        except PageNotAnInteger:
            all_videos =
        except EmptyPage:
            all_videos =

        index         = all_videos.number - 1
        limit         = 5 #limit for show range left and right of number pages
        max_index     = len(paginator.page_range)
        start_index = index - limit if index >= limit else 0
        end_index     = index + limit if index <= max_index - limit else max_index
        page_range     = paginator.page_range[start_index:end_index]
        return page_range

Some Explanations:

1. param: self.queryset this for parameter input with queryset method, egg: models.Video.objects.published()
2. param: self.numb_pages this for number of pagination, egg: int 6 is number per page or from param: paginate_by in generic.ListView.
3. param: self.request_page this for request method with parameter as you want, egg: self.request.GET.get('page')

For Example Implementation of it:

class Video_Index_Views(generic.ListView):
    queryset = models.Video.objects.published().order_by('-created').order_by('-id')
    template_name = 'home_video.html'
    paginate_by = 10

    def get_context_data(self, **kwargs):
        context_data = super(Video_Index_Views, self).get_context_data(**kwargs)
        context_data['page_range'] = Custom_Pagination(self.queryset, self.paginate_by, self.request.GET.get('page')).pagination()
        return context_data

In Your templates..

{% block pagination %}
    {% if is_paginated %}
    <p class="all-paginate">
        <i>Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.</i>
    <ul class="pagination" style="margin-top:5px;">
      <li {% if not page_obj.has_previous %}class="disabled"{% endif %}>
         <a {% if page_obj.has_previous %}href="?page={{ page_obj.previous_page_number }}" aria-label="Previous" {% endif %}><span aria-hidden="true">&laquo;</span></a>
       <li><a href="?page=1">First</a></li>
      {% for linkpage in page_range %}
          {% ifequal linkpage page_obj.number %}
             <li class="active">
               <a>{{ page_obj.number }}<span class="sr-only">(current)</span></a>
          {% else %}
             <li><a href="?page={{ linkpage }}">{{ linkpage }}</a></li>
          {% endifequal %}
      {% endfor %}
      <li><a href="?page={{ page_obj.paginator.num_pages }}">Last</a></li>
      <li {% if not page_obj.has_next %}class="disabled"{% endif %}>
         <a {% if page_obj.has_next %}href="?page={{ page_obj.next_page_number }}" aria-label="Next" {% endif %}><span aria-hidden="true">&raquo;</span></a>
    {% endif %}
{% endblock %}

Hope it usefull..

146 3 3 0