python flask實(shí)現(xiàn)分頁(yè)的示例代碼
結(jié)合mysql數(shù)據(jù)庫(kù)查詢,實(shí)現(xiàn)分頁(yè)效果
@user.route("/user_list",methods=['POST','GET']) def user_list(): p = g.args.get("p", '') #頁(yè)數(shù) show_shouye_status = 0 #顯示首頁(yè)狀態(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)) #總頁(yè)數(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 # 顯示的頁(yè)碼數(shù) pageoffset = 2 # 偏移量 start = 1 #分頁(yè)條開(kāi)始 end = total #分頁(yè)條結(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
如果這里需要進(jìn)行前端模板的拼接的話,可以需要以下代碼(bootstrap)
<ul class="pagination"> {% if datas.show_shouye_status==1%} <li class=''><a href='/user/user_list?p=1'>首頁(yè)</a></li> <li class=''><a href='/user/user_list?p={{datas.p-1}}'>上一頁(yè)</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}}'>下一頁(yè)</a></li> <li class=''><a href='/user/user_list?p={{datas.total}}'>尾頁(yè)</a></li> {%endif%} 共{{datas.total}}頁(yè) </ul>
bootstrap樣式 http://edu.jb51.net/bootstrap/bootstrap-pagination.html
如果是返回給APP端的話,直接返回data數(shù)據(jù)就可以了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python3中關(guān)于cookie的創(chuàng)建與保存
今天小編就為大家分享一篇關(guān)于Python3中關(guān)于cookie的創(chuàng)建與保存的文章,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10Python實(shí)現(xiàn)的HMacMD5加密算法示例
這篇文章主要介紹了Python實(shí)現(xiàn)的HMacMD5加密算法,簡(jiǎn)單說(shuō)明了HMAC-MD5加密算法的概念、原理并結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)HMAC-MD5加密算法的相關(guān)操作技巧,,末尾還附帶了Java實(shí)現(xiàn)HMAC-MD5加密算法的示例,需要的朋友可以參考下2018-04-04