1. 程式人生 > >django 分頁例子

django 分頁例子

django 分頁例子

安裝

python3.6 -m pip install django-pure-pagination==0.3.0

設定

settings.py
INSTALLED_APPS = [
    'pure_pagination',
]

PAGINATION_SETTINGS = {
    'PAGE_RANGE_DISPLAYED': 3,
    'MARGIN_PAGES_DISPLAYED': 2,
    'SHOW_FIRST_PAGE_WHEN_INVALID': True,
}

DISPLAY_PER_PAGE = 15

應用

from pure_pagination import PageNotAnInteger 
from pure_pagination import Paginator 
        try:
            page = self.request.GET.get('page', 1)
        except PageNotAnInteger_He as e:
            page = 1
        p = Paginator_He(self.queryset, getattr(settings, 'DISPLAY_PER_PAGE'), request=self.request)
        asset_list = p.page(page)

        context = {
            "ecs_list": asset_list,
             }

    <div style="text-align:center;">
            <nav class="pagination">

                    <li><a href="{% url "asset:ecs_list" %}?page=1">首頁 </a></li>

                    {% if ecs_list.has_previous %}
                            <li class="long"><a
                                            href="?{{ ecs_list.previous_page_number.querystring }}">上一頁</a>
                            </li>
                    {% endif %}

                    {% for page in ecs_list.pages %}
                            {% if page %}
                                    {% ifequal page ecs_list.number %}
                                            <li class="active"><a href="?{{ page.querystring }}">{{ page }}</a>
                                            </li>
                                    {% else %}
                                            <li><a href="?{{ page.querystring }}" class="page">{{ page }}</a>
                                            </li>
                                    {% endifequal %}
                            {% else %}
                                    <li class="none"><a href="">...</a></li>
                            {% endif %}
                    {% endfor %}
                    {% if ecs_list.has_next %}
                            <li class="long"><a
                                            href="?{{ ecs_list.next_page_number.querystring }}">下一頁</a>
                            </li>
                    {% endif %}
                    <li>
                            <a href="{% url "asset:ecs_list" %}?page={{ ecs_list.paginator.num_pages }}">尾頁 </a>
                    </li>
                    <li><span
                                    style="color: #0a0a0a">總:  {{ ecs_list.paginator.num_pages }} 頁</span>
                    </li>
                    <li><span
                                    style="color: #0a0a0a">  ECS數量: {{ ecs_count }}</span>
                    </li>
            </nav>
    </div>