找回密碼
 註冊
搜索
查看: 1070|回復: 0

[教學] flask render_template mariadb 如何做翻頁(pagination)功能

[複製鏈接]
發表於 2025-7-10 23:48:53 | 顯示全部樓層 |閱讀模式
Push to Facebook
google查了不少文章

發現mariadb 翻頁(pagination)功能 沒人寫
只好土法煉鋼自己寫一個


展示:

flask render_template mariadb 翻頁功能 pagination

flask render_template mariadb  翻頁功能 pagination



admin_view.py內寫入
  1. from app.pagination import pageft
  2. from math import ceil

  3. # 秀出所有紀錄
  4.     count_sql = "select * from net_class"
  5.     cursor.execute(count_sql)
  6.     total = len(cursor.fetchall())

  7.     page_num = request.args.get('page', 1, type=int) #抓取初始頁數
  8.     firstcount = 1 # 初始計數
  9.     displaypg = 10 # 每頁顯示幾筆
  10.     lastpg = ceil(total / displaypg)

  11.     if page_num == 1:
  12.         firstcount = 0
  13.     elif page_num == lastpg:
  14.         firstcount = firstcount * page_num + 1
  15.         displaypg = total % displaypg
  16.     else:
  17.         firstcount = firstcount*page_num+1
  18.              # pageft(初始計數, 總筆數, 每頁顯示幾筆, url_for(def路徑))
  19.     page_nav = pageft(firstcount, total, displaypg, "admin_class")

  20.     sql = "SELECT * FROM net_class ORDER BY num ASC limit "+str(firstcount)+","+str(displaypg)
  21.     print("sql:",sql)

  22.     cursor.execute(sql)
  23.     result = cursor.fetchall()
複製代碼


建立 pagination.py

  1. from flask import request,url_for
  2. from math import ceil

  3. def pageft(firstcount,total,displaypg = 20,url=""):
  4.     if request.args.get('page')== None:
  5.         page = 1
  6.     else:
  7.         page = int(request.args.get('page'))
  8.     if page<=0:
  9.         page=1
  10.     lastpg = ceil(total /displaypg) # 最後頁,也是總頁數

  11.     if page == 1:
  12.         prepg = 1
  13.         firstcount = 1
  14.         nextcount = 1 * displaypg
  15.         nextpg = page + 1
  16.     elif page == lastpg:
  17.         firstcount = (page-1) * displaypg+1
  18.         nextcount = total
  19.         nextpg = page
  20.         prepg = page - 1
  21.     else:
  22.         firstcount = (page-1) * displaypg+1
  23.         nextcount = page * displaypg
  24.         prepg = page - 1
  25.         nextpg = page + 1

  26.     pagenav = "顯示 <B>"+ str(firstcount)  +"</B>-<B>"+ str(min(nextcount ,total)) +"</B> 筆記錄,共 "+str(total)+" 筆"

  27.     if lastpg <= 1:
  28.         return False

  29.     pagenav += " <a href='"+ url_for(url) +"?page=1'>頁首</a> "
  30.     if prepg:
  31.         pagenav += " <a href='"+url_for(url)+"?page="+str(prepg)+"'>上一頁</a> "
  32.     else:
  33.         pagenav += " <a href='"+url_for(url)+"?page="+str(prepg)+"'>上一頁</a> "
  34.     if nextpg :
  35.         pagenav += " <a href='"+url_for(url)+"?page="+ str(nextpg)+"'>下一頁</a>"
  36.     else:
  37.         pagenav += " <a href='"+url_for(url)+"?page="+ str(nextpg)+"'>下一頁</a>"
  38.     pagenav +=" <a href='"+url_for(url)+"?page="+str(lastpg)+"'>頁尾</a> "
  39.     print("pagenav:",pagenav)

  40.     pagenav += " 到第 <select onchange = "window.location.href=this.value" >\n"
  41.     for i in range(lastpg):
  42.         if i+1 == page:
  43.             pagenav +="<option value='"+url_for(url)+"?page="+str(i+1)+"' selected>"+str(i+1)+"</option>\n"
  44.         else:
  45.             pagenav +="<option value='"+url_for(url)+"?page="+str(i+1)+"'>"+str(i+1)+"</option>\n"

  46.     pagenav += "</select> 頁,共 "+str(lastpg)+" 頁";
  47.     return pagenav
複製代碼



company.html 加入
  1. {% if page_nav %}
  2.     <div style="ruby-align: center">{{page_nav | safe}}</div>
  3.     {% endif %}
複製代碼


完成

文章出處: NetYea 網頁設計, 慧晟數位科技, 網站架設

參考文章
https://stackoverflow.com/questi ... rts-to-first-option



您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

Archiver|手機版|小黑屋|TShopping

GMT+8, 2025-8-27 12:36 , Processed in 0.024104 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回復 返回頂部 返回列表