分页插件访问地址:https://github.com/jamespacileo/django-pure-pagination
1.在项目虚拟环境安装分页插件
pip install django-pure-pagination
2.在settings中进行配置
INSTALLED_APPS = (
...
'pure_pagination',
)
3.views.py逻辑编写
导入 from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
def get(self, request):
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,
}
4.模板html页面中设置
{% for course_org in all_orgs.object_list%}
{% for page in all_orgs.pages %}
{% if page %}
{% ifequal page page_obj.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 %}
下一页显示逻辑
在html页面设置{% for course_org in all_orgs.object_list%}
{% if all_orgs.has_previous %} #判断是否有前一页
<li class="long"><a href="?{{ all_orgs.previous_page_number.querystring }}">上一页</a></li>
{% endif %}
循环显示中间页
{% for page in all_orgs.pages %}
{% if page %}
{% ifequal page page_obj.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 %}