Django Component


922 浏览 4 years, 9 months

6.1 bootstrap + django Paginator

版权声明: 转载请注明出处 http://www.codingsoho.com/

bootstrap + django Paginator

https://docs.djangoproject.com/en/2.0/topics/pagination/

Plugin.mixin.py

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

class PaginationMixin(object):
    def get_context_data(self, *args, **kwargs):
        context = super(PaginationMixin, self).get_context_data(*args, **kwargs)

        object_list= context["object_list"]

        getvars = self.request.GET.copy()
        if 'page' in getvars:
            del getvars['page']
        if 'pagenum_perpage' in getvars:
            del getvars['pagenum_perpage']
        if len(getvars.keys()) > 0:
            context['getvars'] = "&%s" % getvars.urlencode()
        else:
            context['getvars'] = ''

        pagenum_perpage = self.request.GET.get('pagenum_perpage', None)
        if pagenum_perpage and pagenum_perpage.isdigit():
            pagenum_perpage = int(pagenum_perpage)
        else:
            pagenum_perpage = 10

        paginator = Paginator(object_list, pagenum_perpage)
        records = None
        page = self.request.GET.get('page')

        try:
            records = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            records = paginator.page(1)
            page = 1
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            records = paginator.page(paginator.num_pages)
            page = paginator.num_pages

        page_value = int(page)
        context["start"] = pagenum_perpage*(page_value-1) + 1
        context["end"] = pagenum_perpage * page_value if not page == paginator.num_pages else object_list.count()
        context["total"] = object_list.count()
        context["total_page"] = paginator.num_pages
        display_pages = []
        for page in range(1,6):
            ipage = ((page_value-1)/5)*5 + page
            if not ipage > paginator.num_pages:
                display_pages.append(ipage)
            else:
                break
        context["pages"] = display_pages
        context["page_highlight"] = page_value
        context["page"] = page_value if page_value else page
        context["pagenum_perpage"] = pagenum_perpage

        context["object_list"] = records

        return context 

Plugin.templates.inluces.pagination.html

{% load i18n %}

<div class="row">
    <div class="col-sm-5">
        <div class="dataTables_info" id="example2_info" role="status" aria-live="polite">{% blocktrans %}Showing {{start}} to {{end}} of {{total}} entries{% endblocktrans %}</div>
        <form method="GET" action="" style="float: left;">
            <label>{% trans "each page" %}
                <select name="pagenum_perpage" " class="input-sm?">
                    <option value="10" {% if pagenum_perpage == 10 %}selected{% endif %}>10</option>
                    <option value="25" {% if pagenum_perpage == 25 %}selected{% endif %}>25</option>
                    <option value="50" {% if pagenum_perpage == 50 %}selected{% endif %}>50</option>
                    <option value="100" {% if pagenum_perpage == 100 %}selected{% endif %}>100</option>
                </select> {% trans 'entries' %}
            </label>   
            <input id="id_des_page" name="page" type="text" value="{{page}}" style="width: 50px; text-align: center;"> / {{total_page}}{% trans 'Page' %}
            <input type='submit' class='btn btn-primary' value="{% trans 'Go!' %}">
        </form> 
    </div>
    <div class="col-sm-7">
        <div class="dataTables_paginate paging_simple_numbers pull-right" id="example2_paginate" >
            <ul class="pagination" style="margin: 0px;">
                <li class="paginate_button previous {% if not object_list.has_previous %}disabled{% endif %}" id="example2_previous">
                    <a href="{% if object_list.has_previous %}?page={{ object_list.previous_page_number }}&pagenum_perpage={{pagenum_perpage}}{% endif %}{{getvars}}" aria-controls="example2" data-dt-idx="0" tabindex="0">{% trans 'previous' %}</a>
                </li>
                {% for page in pages %}
                <li class="paginate_button {% if page == page_highlight %}active{% endif %}">
                    <a href="?page={{page}}&pagenum_perpage={{pagenum_perpage}}{{getvars}}" aria-controls="example2" data-dt-idx="{{forloop.count}}" tabindex="0">{{page}}</a>
                </li>
                {% endfor %}
                <li class="paginate_button next {% if not object_list.has_next %}disabled{% endif %}" id="example2_next">
                    <a href="{% if object_list.has_next %}?page={{ object_list.next_page_number }}&pagenum_perpage={{pagenum_perpage}}{% endif %}{{getvars}}" aria-controls="example2" data-dt-idx="7" tabindex="0">{% trans 'next' %}</a>
                </li>
            </ul>
        </div>
    </div>
</div>