在我们平时浏览网页时,经常会遇到网页里条目很多的情形,这时就会用到分页展示的功能。那么,在Django中,是如何实现网页分类的功能的呢?答案是Paginator类。
本次分享讲具体展示如何利用Django的Paginator类来实现网页的分页展示功能。
首先我们要展示的内容是159首陶渊明的诗歌,它们储存在'/home/vagrant/poem.txt'文件中。
默认的不分页的网页(page.html)如下:
其模板的代码如下:
<ul>
{% for contact in contacts %}
<li>{{contact}}<br><br></li>
{% endfor %}
</ul>
<div>
<center>
{% if contacts.has_previous %}
<a href="?page={{ contacts.previous_page_number }}"> 上一页</a>
{% endif %}
<span>
{% for p in contacts.paginator.page_range %}
{% ifequal p contacts.number %}
<span class="current">{{p}}</span>
{% else %}
<a href="?page={{p}}" title="第{{p}}页"><button>{{p}}</button></a>
{% endifequal %}
{% endfor %}
</span>
{% if contacts.has_next %}
<a href="?page={{ contacts.next_page_number }}">下一页 > </a>
{% endif %}
<a>[page:{{ contacts.number }}/{{ contacts.paginator.num_pages }}]</a>
</center>
</div>
<br>
<form action="/fenye/" method="get">
<center>
<select name='items'>
<option value='10' checked="checked">10 items each page</option>
<option value='15'>15 items each page</option>
<option value='20'>20 items each page</option>
</select>
<input type="submit" value="select">
</center>
</form>
该网页中的数字按钮可连接到对应的页码,当然也可以用‘下一页’链接。也可以自由选择每页需要显示的条目数量,如下:
选择好条目数,在点击select按钮,可链接到如下页面:
这个页面(fenye.html)显示每页10条,可以点击按钮‘Return To Original Page’回到刚才的页面(page.html)。该页面(fenye.html)的模板代码如下:
<ul>
{% for contact in contacts %}
<li>{{contact}}<br><br></li>
{% endfor %}
</ul>
<div>
<center>
{% if contacts.has_previous %}
<a href="?page={{ contacts.previous_page_number }}"> 上一页</a>
{% endif %}
<span>
{% for p in contacts.paginator.page_range %}
{% ifequal p contacts.number %}
<span class="current">{{p}}</span>
{% else %}
<a href="?page={{p}}" title="第{{p}}页"><button>{{p}}</button></a>
{% endifequal %}
{% endfor %}
</span>
{% if contacts.has_next %}
<a href="?page={{ contacts.next_page_number }}">下一页 > </a>
{% endif %}
<a>[page:{{ contacts.number }}/{{ contacts.paginator.num_pages }}]</a>
</center>
</div>
<center>
<br>
<button onclick="window.location.href='http://localhost:8000/page'">Return To Original Page</button>
</center>
最后,我们的后端views.py代码如下:
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.utils.datastructures import MultiValueDictKeyError
from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage
'''
遇到不懂的问题?Python学习交流群:821460695满足你的需求,资料都已经上传群文件,可以自行下载!
'''
#show items in default
def output(request):
# get items show on the page from poems.txt
with open('/home/vagrant/poems.txt', 'r') as f:
text = f.readlines()
items_each_page = 15 # how many items in each page
paginator = Paginator(text, items_each_page)
page = request.GET.get('page') # get 'page'
try:
contacts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
contacts = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
contacts = paginator.page(paginator.num_pages)
return render_to_response('page.html',{'contacts': contacts})
#show items by your choice
def output2(request):
with open('/home/vagrant/poems.txt', 'r') as f:
text = f.readlines()
try:
#try to get 'items', if None or failed, do nothing.
a = request.GET.get('items')
if str(a) != 'None':
with open('/home/vagrant/num.txt', 'w') as f:
f.write(a)
except:
pass
with open('/home/vagrant/num.txt','r') as f:
items_each_page = int(f.read())
paginator = Paginator(text, items_each_page)
page = request.GET.get('page')
try:
contacts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
contacts = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
contacts = paginator.page(paginator.num_pages)
return render_to_response('fenye.html',{'contacts': contacts})
这样,我们就基本实现了在Django中网页分页展示的功能,而且能够自己选择每一页需要展示的条目的数量,这显然是非常方便使用的。