1. 程式人生 > >django 分頁django-pure-pagination

django 分頁django-pure-pagination

pre 自帶 provide ... ani log 一個 esp https

雖然django自帶了一個paginator,但不是很方便,我們使用django-pure-pagination

github地址https://github.com/jamespacileo/django-pure-pagination.git

裏面有教程,這裏簡要的說明一下

第一步下載

兩種方式

一pip安裝pip install django-pure-pagination

二源碼安裝git clone https://github.com/jamespacileo/django-pure-pagination.git

cd django-pure-pagination

python setup.py install

兩者選其一

第二步使用

在settings中添加

INSTALLED_APPS = (
    ...
    pure_pagination,
)

這裏在settings中加入一些分頁配置,

PAGINATION_SETTINGS = {
    PAGE_RANGE_DISPLAYED: 10,
    MARGIN_PAGES_DISPLAYED: 2,

    SHOW_FIRST_PAGE_WHEN_INVALID: True,
}

views

# views.py
from django.shortcuts import render_to_response

from pure_pagination import Paginator, EmptyPage, PageNotAnInteger def index(request): try: page = request.GET.get(page, 1) except PageNotAnInteger: page = 1 objects = [john, edward, josh, frank] # Provide Paginator with the request object for complete querystring generation
p = Paginator(objects, request=request) people = p.page(page) return render_to_response(index.html, { people: people, }

html

{# index.html #}
{% extends ‘base.html‘ %}

{% block content %}

{% for person in people.object_list %}
    <div>
        First name: {{ person }}
    </div>
{% endfor %}

{# The following renders the pagination html #}
<div id="pagination">
    {{ people.render }}
</div>

{% endblock %}

還有什麽不明白的可以上github上看

django 分頁django-pure-pagination