Django自带有分页的两个类,但是用起来没有第三方这个分页模块方便,下面介绍一下这个模块的使用方法。
1. 安装模块:
pip install django-pure-pagination
2. 配置
在settings中
INSTALLED_APPS = ( ... 'pure_pagination', )
还可以在settings中加入一些分页配置(未使用)
PAGINATION_SETTINGS = { 'PAGE_RANGE_DISPLAYED': 10, 'MARGIN_PAGES_DISPLAYED': 2, 'SHOW_FIRST_PAGE_WHEN_INVALID': True, }
3. 使用
后端:
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger class OrgCourse(View): def get(self, request, nid): current_page = 'course' is_has_fav = False if request.user.is_authenticated(): if UserFavorite.objects.filter(user=request.user, fav_id=nid, fav_type=2): is_has_fav = True org_o = CourseOrg.objects.get(id=nid) # 机构下的课程 course_list = org_o.get_course() # 对课程进行分页 try: page = request.GET.get('page', 1) except PageNotAnInteger: page = 1 p = Paginator(course_list, 4, request=request) course_es = p.page(page) return render(request, 'org_course.html', context=locals())
前段:
<div class="pageturn"> <ul class="pagelist"> {% if org_es.has_previous %} <li class="long"><a href="?{{ org_es.previous_page_number.querystring }}">上一页</a></li> {% endif %} {% for page in org_es.pages %} {% if page %} {% ifequal page org_es.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 org_es.has_next %} <li class="long"><a href="?{{ org_es.next_page_number.querystring }}">下一页</a></li> {% endif %} </ul> </div>