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è)條開始
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)文章
Python學(xué)習(xí)思維導(dǎo)圖(必看篇)
下面小編就為大家?guī)硪黄狿ython學(xué)習(xí)思維導(dǎo)圖(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
一些讓Python代碼簡(jiǎn)潔的實(shí)用技巧總結(jié)
隨著項(xiàng)目代碼行數(shù)的增加,不可避免的遇到軟件架構(gòu)腐敗的問題,所以如何寫出簡(jiǎn)潔的代碼至關(guān)重要,這篇文章主要給大家介紹了一些讓Python代碼簡(jiǎn)潔的實(shí)用技巧,需要的朋友可以參考下2021-08-08
Pytorch如何把Tensor轉(zhuǎn)化成圖像可視化
這篇文章主要介紹了Pytorch如何把Tensor轉(zhuǎn)化成圖像可視化問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
利用pandas進(jìn)行數(shù)據(jù)清洗的方法
本文主要介紹了利用pandas進(jìn)行數(shù)據(jù)清洗的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
python自定義函數(shù)實(shí)現(xiàn)一個(gè)數(shù)的三次方計(jì)算方法
今天小編就為大家分享一篇python自定義函數(shù)實(shí)現(xiàn)一個(gè)數(shù)的三次方計(jì)算方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Python實(shí)現(xiàn)根據(jù)日期獲取當(dāng)天凌晨時(shí)間戳的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)根據(jù)日期獲取當(dāng)天凌晨時(shí)間戳的方法,涉及Python針對(duì)日期與時(shí)間戳的相關(guān)轉(zhuǎn)換、運(yùn)算等操作技巧,需要的朋友可以參考下2019-04-04
Python3使用騰訊云文字識(shí)別(騰訊OCR)提取圖片中的文字內(nèi)容實(shí)例詳解
這篇文章主要介紹了Python3使用騰訊云文字識(shí)別(騰訊OCR)提取圖片中的文字內(nèi)容方法詳解,需要的朋友可以參考下2020-02-02

