django分页器的一些思考
Posted rongze-blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django分页器的一些思考相关的知识,希望对你有一定的参考价值。
今天用django写了两个分页器,实现的思路并不一样,发现二者各有利弊
第一种方法:从数据库获取数据,在后端用字符串格式化的方法做成html语句,然后传到前端进行渲染
代码如下
views
username = req.session[‘username‘] currunt_page=req.GET.get("page", 1) currunt_page=int(currunt_page) end_page=currunt_page*10 start_page=end_page-10 class_list = models.Class.objects.all()[start_page:end_page] total_count=models.Class.objects.all().count() page_num, odd=divmod(total_count, 10) page_list=[] if odd != 0: page_num+=1 if currunt_page <= 5: page_start = 1 page_end = 11 elif currunt_page >= page_num - 5 and currunt_page<=page_num: page_start = page_num - 10 page_end = page_num else: page_start = currunt_page - 5 page_end = currunt_page + 5 if currunt_page==1: page_list.append("<a href=‘/class_manage.html?page=%s‘>上一页</a>" % (currunt_page)) else: page_list.append("<a href=‘/class_manage.html?page=%s‘>上一页</a>" % (currunt_page-1)) for i in range(page_start, page_end+1): if i == currunt_page: page_list.append("<a class=‘active‘ href=‘/class_manage.html?page=%s‘>%s</a>"%(i,i,)) else: page_list.append("<a href=‘/class_manage.html?page=%s‘>%s</a>"%(i,i,)) if currunt_page==page_num: page_list.append("<a href=‘/class_manage.html?page=%s‘>下一页</a>" % (currunt_page)) else:page_list.append("<a href=‘/class_manage.html?page=%s‘>下一页</a>" % (currunt_page+1)) pager="".join(page_list) return render(req, ‘class_manage.html‘, {‘username‘: username,"class_list":class_list,"page_list":pager})
html
<div class="page"> {{ page_list|safe }} </div>
第二种方法:在后端生成数据包传到前端,使用for循环进行渲染(传输数据时,假设current_page和page_list已构造成一个字典)
user = request.session.get("user") cla_list = models.Class.objects.all() count = cla_list.count() current_page = request.GET.get("page", 1) current_page=int(current_page) opt= request.GET.get("opt",None) page_num, last = divmod(count, 10) page_list = [] if last!=0: page_num += 1 if current_page != 1 and current_page != page_num: if opt == "‘next‘": current_page+=1 elif opt == "‘last‘": current_page-=1 if current_page == 1 and opt == "‘next‘": current_page += 1 elif current_page == page_num and opt == "‘last‘": current_page -= 1 if current_page<=5: for i in range(1, 11): page_list.append(i) elif current_page>=page_num-5: for i in range(page_num-10, page_num+1): page_list.append(i) else: for i in range(current_page-5, current_page+6): page_list.append(i) cla_list_send = [] for item in cla_list[(current_page-1)*10:current_page*10]: cla_list_send.append(item) return render(request,"classes.html",{"user":user, "cla_list":cla_list_send,"page_list":page_list,"current_page":current_page})
html
<a href="/cla_manage?page={{ current_page }};opt=‘last‘">上一页</a> <span> {% for i in page_list %} <a href="/cla_manage?page={{ i }}">{{ i }}</a> {% endfor %} </span> <a href="/cla_manage?page={{ current_page }};opt=‘next‘">下一页</a>
由于这里只对两种思路进行探讨,所以只选取了部分代码,所以希望看到这篇文章的人不必过于关注代码细节
下面讨论两种方式的利弊:
1. 从复杂度来说:这两种方式在算法复杂度上相差不大(这里要注意django在前端代码中写的for循环语句,本质还是在后端先进行了迭代,然后django会自己生成html代码然后发送到前端)
2. 从封装调用来说:如果在后端将二者进行封装,在接口方面而这需要传入的参数相差不大,但是在调用时明显第二种不够友好,因为对于前端来说要调用许多数据
3. 从亲后端分离来说:第二种做到了前后端分离,而第一种需要在后端写html代码
自己更倾向于第一种,当然分页器第一次接触,还未进行更加深入的思考,这篇文章来日还要再进行总结。
以上是关于django分页器的一些思考的主要内容,如果未能解决你的问题,请参考以下文章