Django学习(5)优雅地分页展示网页
在我们平时浏览网页时,经常会遇到网页里条目很多的情形,这时就会用到分页展示的功能。那么,在Django中,是如何实现网页分类的功能的呢?答案是Paginator类。
本次分享讲具体展示如何利用Django的Paginator类来实现网页的分页展示功能。
首先我们要展示的内容是159首陶渊明的诗歌,它们储存在'/home/vagrant/poem.txt'文件中。
默认的不分页的网页(page.html)如下:
其模板的代码如下:
1 <ul> 2 {% for contact in contacts %} 3 <li>{{contact}}<br><br></li> 4 {% endfor %} 5 </ul> 6 7 <div> 8 <center> 9 {% if contacts.has_previous %} 10 <a href="?page={{ contacts.previous_page_number }}"> 上一页</a> 11 {% endif %} 12 <span> 13 {% for p in contacts.paginator.page_range %} 14 {% ifequal p contacts.number %} 15 <span class="current">{{p}}</span> 16 {% else %} 17 <a href="?page={{p}}" title="第{{p}}页"><button>{{p}}</button></a> 18 {% endifequal %} 19 {% endfor %} 20 </span> 21 {% if contacts.has_next %} 22 <a href="?page={{ contacts.next_page_number }}">下一页 > </a> 23 {% endif %} 24 <a>[page:{{ contacts.number }}/{{ contacts.paginator.num_pages }}]</a> 25 </center> 26 </div> 27 28 <br> 29 <form action="/fenye/" method="get"> 30 <center> 31 <select name='items'> 32 <option value='10' checked="checked">10 items each page</option> 33 <option value='15'>15 items each page</option> 34 <option value='20'>20 items each page</option> 35 </select> 36 <input type="submit" value="select"> 37 </center> 38 </form>
该网页中的数字按钮可连接到对应的页码,当然也可以用‘下一页’链接。也可以自由选择每页需要显示的条目数量,如下:
选择好条目数,在点击select按钮,可链接到如下页面:
这个页面(fenye.html)显示每页10条,可以点击按钮‘Return To Original Page’回到刚才的页面(page.html)。该页面(fenye.html)的模板代码如下:
1 <ul> 2 {% for contact in contacts %} 3 <li>{{contact}}<br><br></li> 4 {% endfor %} 5 </ul> 6 7 <div> 8 <center> 9 {% if contacts.has_previous %} 10 <a href="?page={{ contacts.previous_page_number }}"> 上一页</a> 11 {% endif %} 12 <span> 13 {% for p in contacts.paginator.page_range %} 14 {% ifequal p contacts.number %} 15 <span class="current">{{p}}</span> 16 {% else %} 17 <a href="?page={{p}}" title="第{{p}}页"><button>{{p}}</button></a> 18 {% endifequal %} 19 {% endfor %} 20 </span> 21 {% if contacts.has_next %} 22 <a href="?page={{ contacts.next_page_number }}">下一页 > </a> 23 {% endif %} 24 <a>[page:{{ contacts.number }}/{{ contacts.paginator.num_pages }}]</a> 25 </center> 26 </div> 27 <center> 28 <br> 29 <button onclick="window.location.href='http://localhost:8000/page'">Return To Original Page</button> 30 </center>
最后,我们的后端views.py代码如下:
1 from django.http import HttpResponse 2 from django.shortcuts import render_to_response 3 from django.utils.datastructures import MultiValueDictKeyError 4 from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage 5 6 #show items in default 7 def output(request): 8 # get items show on the page from poems.txt 9 with open('/home/vagrant/poems.txt', 'r') as f: 10 text = f.readlines() 11 12 items_each_page = 15 # how many items in each page 13 paginator = Paginator(text, items_each_page) 14 page = request.GET.get('page') # get 'page' 15 try: 16 contacts = paginator.page(page) 17 except PageNotAnInteger: 18 # If page is not an integer, deliver first page. 19 contacts = paginator.page(1) 20 except EmptyPage: 21 # If page is out of range (e.g. 9999), deliver last page of results. 22 contacts = paginator.page(paginator.num_pages) 23 return render_to_response('page.html',{'contacts': contacts}) 24 25 #show items by your choice 26 def output2(request): 27 with open('/home/vagrant/poems.txt', 'r') as f: 28 text = f.readlines() 29 30 try: 31 #try to get 'items', if None or failed, do nothing. 32 a = request.GET.get('items') 33 if str(a) != 'None': 34 with open('/home/vagrant/num.txt', 'w') as f: 35 f.write(a) 36 except: 37 pass 38 39 with open('/home/vagrant/num.txt','r') as f: 40 items_each_page = int(f.read()) 41 42 paginator = Paginator(text, items_each_page) 43 page = request.GET.get('page') 44 try: 45 contacts = paginator.page(page) 46 except PageNotAnInteger: 47 # If page is not an integer, deliver first page. 48 contacts = paginator.page(1) 49 except EmptyPage: 50 # If page is out of range (e.g. 9999), deliver last page of results. 51 contacts = paginator.page(paginator.num_pages) 52 return render_to_response('fenye.html',{'contacts': contacts})
这样,我们就基本实现了在Django中网页分页展示的功能,而且能够自己选择每一页需要展示的条目的数量,这显然是非常方便使用的。
本次分享到此结束,如有问题,欢迎大家交流与批评~~