python flask實現(xiàn)分頁的示例代碼
更新時間:2018年08月02日 09:30:20 作者:徐代龍
這篇文章主要介紹了python flask實現(xiàn)分頁的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
結(jié)合mysql數(shù)據(jù)庫查詢,實現(xiàn)分頁效果
@user.route("/user_list",methods=['POST','GET']) def user_list(): p = g.args.get("p", '') #頁數(shù) show_shouye_status = 0 #顯示首頁狀態(tài) if p =='': p=1 else: p=int(p) if p > 1: show_shouye_status = 1 mdb = db_session() limit_start = (int(p)-1)*10#起始 sql ="select * from page_text limit {0},10".format(limit_start) user_list=mdb.getMany(sql) sql="select count(id) as total from page_text" count = mdb.getOne(sql)['total'] #總記錄 total = int(math.ceil(count/10.0)) #總頁數(shù) dic = get_page(total,p) datas={ 'user_list':user_list, 'p': int(p), 'total': total, 'show_shouye_status': show_shouye_status, 'dic_list': dic } return render_template("user_list.html",datas=datas)
其中g(shù)et_page為封裝的方法:
def get_page(total,p): show_page = 5 # 顯示的頁碼數(shù) pageoffset = 2 # 偏移量 start = 1 #分頁條開始 end = total #分頁條結(jié)束 if total > show_page: if p > pageoffset: start = p - pageoffset if total > p + pageoffset: end = p + pageoffset else: end = total else: start = 1 if total > show_page: end = show_page else: end = total if p + pageoffset > total: start = start - (p + pageoffset - end) #用于模版中循環(huán) dic = range(start, end + 1) return dic
如果這里需要進行前端模板的拼接的話,可以需要以下代碼(bootstrap)
<ul class="pagination"> {% if datas.show_shouye_status==1%} <li class=''><a href='/user/user_list?p=1'>首頁</a></li> <li class=''><a href='/user/user_list?p={{datas.p-1}}'>上一頁</a></li> {%endif%} {% for dic in datas.dic_list %} {% if dic==datas.p%} <li class="active"><a href="/user/user_list?p={{dic}}" rel="external nofollow" rel="external nofollow" >{{dic}}</a></li> {%else%} <li><a href="/user/user_list?p={{dic}}" rel="external nofollow" rel="external nofollow" >{{dic}}</a></li> {%endif%} {%endfor%} {% if datas.p < datas.total%} <li class=''><a href='/user/user_list?p={{datas.p+1}}'>下一頁</a></li> <li class=''><a href='/user/user_list?p={{datas.total}}'>尾頁</a></li> {%endif%} 共{{datas.total}}頁 </ul>
bootstrap樣式 http://edu.jb51.net/bootstrap/bootstrap-pagination.html
如果是返回給APP端的話,直接返回data數(shù)據(jù)就可以了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python3中關(guān)于cookie的創(chuàng)建與保存
今天小編就為大家分享一篇關(guān)于Python3中關(guān)于cookie的創(chuàng)建與保存的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10