一、App说明
该App用户Django的数据分页功能
二、安装
pip install django-pure-pagination
三、使用方法
(1)settings注册
INSTALLED_APPS = (
...
‘pure_pagination‘,
)
(2)settings中设置全局变量
PAGINATION_SETTINGS = {
‘PAGE_RANGE_DISPLAYED‘: 10,#显示与当前页面相邻页面数,默认是10
‘MARGIN_PAGES_DISPLAYED‘: 2,#显示第一页和最后一页相邻的页数,默认是2
‘SHOW_FIRST_PAGE_WHEN_INVALID‘: True,#当“无效”时将“显示第一页”设置为“真”,当提供“无效”页而不是404错误时只显示第一页
}
(3)view.py
1 # views.py 2 from django.shortcuts import render_to_response 3 4 from pure_pagination import Paginator, EmptyPage, PageNotAnInteger 5 6 7 def index(request): 8 9 try: 10 page = request.GET.get(‘page‘, 1) 11 except PageNotAnInteger: 12 page = 1 13 14 objects = [‘john‘, ‘edward‘, ‘josh‘, ‘frank‘] 15 16 # Provide Paginator with the request object for complete querystring generation 17 18 p = Paginator(objects, request=request) 19 20 people = p.page(page) 21 22 return render_to_response(‘index.html‘, { 23 ‘people‘: people, 24 }
(4)template样式
{% load i18n %} <div class="pagination"> {% if page_obj.has_previous %} <a href="?{{ page_obj.previous_page_number.querystring }}" class="prev">‹‹ {% trans "previous" %}</a> {% else %} <span class="disabled prev">‹‹ {% trans "previous" %}</span> {% endif %} {% for page in page_obj.pages %} {% if page %} {% ifequal page page_obj.number %} <span class="current page">{{ page }}</span> {% else %} <a href="?{{ page.querystring }}" class="page">{{ page }}</a> {% endifequal %} {% else %} ... {% endif %} {% endfor %} {% if page_obj.has_next %} <a href="?{{ page_obj.next_page_number.querystring }}" class="next">{% trans "next" %} ››</a> {% else %} <span class="disabled next">{% trans "next" %} ››</span> {% endif %} </div>