Django使用内置paginator类分页

Django分页是比较方便的,我们可以使用内置的一个paginator类。

这里以查询图书为例,以下是主要代码:

---------------------------------------------------------------------------------------------------

views.py

#导入方法PageNotAnInteger, Paginator, InvalidPage, EmptyPage

from django.core.paginator import PageNotAnInteger, Paginator, InvalidPage, EmptyPage

def  list(request):

books = Book.objects.all() #之前需要从models中导入Book

after_range_num = 5        #当前页前显示5页

befor_range_num = 4       #当前页后显示4页

try:                     #如果请求的页码少于1或者类型错误,则跳转到第1页

page = int(request.GET.get("page",1))

if page < 1:

page = 1

except ValueError:

page = 1

paginator = Paginator(books,2)   # 设置books在每页显示的数量,这里为2

try:                     #跳转到请求页面,如果该页不存在或者超过则跳转到尾页

books_list = paginator.page(page)

except(EmptyPage,InvalidPage,PageNotAnInteger):

books_list = paginator.page(paginator.num_pages)

if page >= after_range_num:

page_range = paginator.page_range[page-after_range_num:page+befor_range_num]

else:

page_range = paginator.page_range[0:int(page)+befor_range_num]

return render-to_response('book_list.html',{'books':books_list,'page_range':page_range})

-----------------------------------------------------------------------------------------------

Templates 视图

book_list.html

{% for book in books.object_list %}

{{ book.id }}  |   {{ book.name }} <br />

{% endfor %}

<div>

{% if books.has_previous %}

<a href="?page={{ books.previous_page_number }}">&lt; 上一页</a>

{% endif %}

<span>

{% for p in page_range %}

{% ifequal p books.number %}

<span class="current">{{p}}</span>

{% else %}

<a href="?page={{p}}" title="第{{p}}页">{{p}}</a>

{% endifequal %}

{% endfor %}

</span>

{% if books.has_next %}

<a href="?page={{ books.next_page_number }}">下一页 &gt; </a>

{% endif %}

<a >[第{{ books.number }}/{{ books.paginator.num_pages }}页]</a>

</div>

____________________________________________________________________________________________

到这里已经完成了Django分页,很简单吧?如果有不太懂的就动手实践一下吧。

当然了,上面的book_list.html中有加CSS样式,你可以去除或者写自己喜欢的样式,不过还是附上css代码(CSS样式来自网络):

CSS:

.manu { PADDING-RIGHT: 3px; PADDING-LEFT: 3px; PADDING-BOTTOM: 3px; MARGIN: 3px; PADDING-TOP: 3px; TEXT-ALIGN: center; font-size:12px;}

.manu A {BORDER-RIGHT: #eee 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #eee 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #eee 1px solid; COLOR: #036cb4; PADDING-TOP: 2px; BORDER-BOTTOM: #eee 1px solid; TEXT-DECORATION: none

}

.manu A:hover { BORDER-RIGHT: #999 1px solid; BORDER-TOP: #999 1px solid; BORDER-LEFT: #999 1px solid; COLOR: #666; BORDER-BOTTOM: #999 1px solid}

.manu A:active {BORDER-RIGHT: #999 1px solid; BORDER-TOP: #999 1px solid; BORDER-LEFT: #999 1px solid; COLOR: #666; BORDER-BOTTOM: #999 1px solid}

.manu .current {BORDER-RIGHT: #036cb4 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #036cb4 1px solid; PADDING-LEFT: 5px; FONT-WEIGHT: bold; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #036cb4 1px solid; COLOR: #fff; PADDING-TOP: 2px; BORDER-BOTTOM: #036cb4 1px solid; BACKGROUND-COLOR: #036cb4

}

.manu .disabled {BORDER-RIGHT: #eee 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #eee 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #eee 1px solid; COLOR: #ddd; PADDING-TOP: 2px; BORDER-BOTTOM: #eee 1px solid

}

你可能感兴趣的:(Django使用内置paginator类分页)