安装:
pip install django-pure-pagination
具体步骤搜索GitHub里面详情。
class OrgView(View): """ 课程机构列表功能 """ def get(self, request): all_orgs = CourseOrg.objects.all() # 课程机构 org_nums = all_orgs.count() # 记录多少家 # 城市 all_citys = CityDict.objects.all() # 课程机构进行分页 try: page = request.GET.get('page', 1) except PageNotAnInteger: page = 1 p = Paginator(all_orgs, 5, request=request) orgs = p.page(page) return render(request, 'org-list.html', { "all_orgs": orgs, "all_citys": all_citys, 'org_nums': org_nums, })
通过传过来的orgs,自动生成页脚
{# 分页#} <div class="pageturn"> <ul class="pagelist"> {# 如果有,获取上一页 #} {% if all_orgs.has_previous %} <li class="long"><a href="?{{ all_orgs.previous_page_number.querystring }}" class="page">上一页</a></li> {% endif %} {# 判断剩余页面的显示方法: #} {% for page in all_orgs.pages %} {% if page %} {# 判断page和 all_orgs.number是否相等 : } {% ifequal page all_orgs.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 all_orgs.has_next %} <li class="long"><a href="?{{ all_orgs.next_page_number.querystring }}">下一页</a></li> {% endif %} </ul> </div>
总结
切记:做分页获取数据要调用 .object_list
{% for course_org in all_orgs.object_list %}