Django-分頁
1.創(chuàng)建對象
Paginator 對象的 page()方法返回 Page 對象,不需要手動構(gòu)造
2.屬性
object_list:當前頁上所有對象的列表
number:當前頁的序號昧绣,從 1 開始
paginator:當前 page 對象相關(guān)的 Paginator 對象
3.方法
has_next():如果有下一頁返回 True
has_previous():如果有上一頁返回 True
has_other_pages():如果有上一頁或下一頁返回 True
next_page_number():返回下一頁的頁碼亩进,如果下一頁不存在,拋出 InvalidPage 異常
previous_page_number():返回上一頁的頁碼咒程,如果上一頁不存在五嫂,拋出InvalidPage異常
len():返回當前頁面對象的個數(shù)
迭代頁面對象:訪問當前頁面中的每個對象
4.示例
views
def view_post(request,pIndex):
# try:
uadmin = request.session['uadmin']
authorid = adminid.objects.get(admin = uadmin)
authorlist = post.objects.filter(author = authorid)
p = Paginator(authorlist, 1)
print(authorlist)
if pIndex == '':
pIndex = int(pIndex)
list = p.page(pIndex)
plist = p.page_range
# print('hobby', shrlist[0].shrname)
# hero = HeroInfo.objects.get
return render(request, 'invitation/view_post.html', {'request':request, 'authorlist': list,'plist':plist,'pIndex':pIndex})
# except Exception as e:
# return redirect(reverse('login:login'))
html
<ul class="mr-list">
{% for item in authorlist%}
<li>標題:{{ item.title }}</li>
<li>類型:{{ item.classify }}</li>
<li>內(nèi)容~~~</li>
{%autoescape off%}
<textarea style="resize:none;width:500px; height:210px ">{{ item.content }}</textarea>
{%endautoescape%}
<li>發(fā)帖時間:{{ item.posted_time }}<li>
{% endfor %}
{% if authorlist.has_previous %}
<a href="/invitation/view_post/{{ authorlist.previous_page_number }}/">上一頁 </a>
{% else %}
上一頁
{% endif %}
{% for pindex in plist %}
{% if pindex == pIndex %}
{{ pindex }}
{% else %}
<a href="/invitation/view_post/{{ pindex }}/">{{ pindex }}</a>
{% endif %}
{% endfor %}
{% if authorlist.has_next %}
<a href="/invitation/view_post/{{ authorlist.next_page_number }}/">下一頁 </a>
{% else %}
下一頁
{% endif %}
</ul>
應用url
url(r'^view_post/(\d+)/$',views.view_post,name='view_post'),