分頁(yè)插件訪問(wèn)地址:https://github.com/jamespacileo/django-pure-pagination
1.在項(xiàng)目虛擬環(huán)境安裝分頁(yè)插件
pip install django-pure-pagination
2.在settings中進(jìn)行配置
INSTALLED_APPS = (
...
'pure_pagination',
)
3.views.py邏輯編寫(xiě)
導(dǎo)入 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) #分頁(yè)
return render(request, "org-list.html', {
'all_orgs': orgs,
}
4.模板html頁(yè)面中設(shè)置
{% 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 %}
下一頁(yè)顯示邏輯
在html頁(yè)面設(shè)置{% for course_org in all_orgs.object_list%}
{% if all_orgs.has_previous %} #判斷是否有前一頁(yè)
<li class="long"><a href="?{{ all_orgs.previous_page_number.querystring }}">上一頁(yè)</a></li>
{% endif %}
循環(huán)顯示中間頁(yè)
{% 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 %}
下一頁(yè)顯示邏輯
{% if all_orgs.has_next %
<li class="long"><a href="?{{ all_orgs.next_page_number.querystring }}">下一頁(yè)</a></li>
{% endif %}