后端數(shù)據(jù)庫(kù)分頁(yè)是根據(jù)limit實(shí)現(xiàn)的
10余年的荔浦網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營(yíng)銷(xiāo)型網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶(hù)設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整荔浦建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“荔浦網(wǎng)站設(shè)計(jì)”,“荔浦網(wǎng)站推廣”以來(lái),每個(gè)客戶(hù)項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。第一種寫(xiě)法: limit 每頁(yè)的個(gè)數(shù) offset 起始位置
select id,name,sex,tel,addr from user limit 1 offset 1;select * from student limit pageSize offset (pageNumber-1)*pageSize第二種寫(xiě)法: limit 起始位置,每頁(yè)的個(gè)數(shù)
select id,name from user limit 1,2;select * from studnet limit (pageNumber-1)*pageSize,pageSize當(dāng)數(shù)據(jù)量比較大的時(shí)候分頁(yè)采用子查詢(xún)的方式
select id,name from user where id >= (select id from user order by id limit 1,1) limit 2;select id,name from user where id >= (select id from user order by id limit 起始位置,1) limit 每頁(yè)的個(gè)數(shù);Python代碼中實(shí)現(xiàn)后臺(tái)數(shù)據(jù)分頁(yè)
views.py文件
Page()函數(shù)是mypage.py里邊類(lèi)的方法
user_count() 是獲取mysql 數(shù)據(jù)總數(shù)的方法
def index(request): page_num = request.GET.get("page") login_user = request.session.get('user') count = user_count() per_page = 2 page_obj = Page(page_num, count,url_prefix="/users/",per_page=2, max_page=4,) page_num = page_obj.num users = mysql_page(per_page,page_num) page_html = page_obj.page_html() return render(request,'static/index.html',{"users": users,"login_user": login_user,"page_html": page_html})mypage.py
class Page(): def __init__(self, page_num, total_count, url_prefix, per_page, max_page=4): """ :param page_num: 當(dāng)前頁(yè)碼數(shù) :param total_count: 數(shù)據(jù)總數(shù) :param url_prefix: a標(biāo)簽href的前綴 :param per_page: 每頁(yè)顯示多少條數(shù)據(jù) :param max_page: 頁(yè)面上最多顯示幾個(gè)頁(yè)碼 """ self.url_prefix = url_prefix self.max_page = max_page # 每一頁(yè)顯示多少條數(shù)據(jù) # 總共需要多少頁(yè)碼來(lái)展示 total_page, m = divmod(total_count, per_page) if m: total_page += 1 self.total_page = total_page try: page_num = int(page_num) # 如果輸入的頁(yè)碼數(shù)超過(guò)了大的頁(yè)碼數(shù),默認(rèn)返回最后一頁(yè) if page_num > total_page: page_num = total_page except Exception as e: # 當(dāng)輸入的頁(yè)碼不是正經(jīng)數(shù)字的時(shí)候 默認(rèn)返回第一頁(yè)的數(shù)據(jù) page_num = 1 self.page_num = page_num # 頁(yè)面上總共展示多少頁(yè)碼 if total_page < self.max_page: self.max_page = total_page @property def num(self): return self.page_num def page_html(self): # 自己拼接分頁(yè)的HTML代碼 html_str_list = [] # 加上第一頁(yè) html_str_list.append('models,py
def mysql_page(per_page,page): start = (page - 1 ) * per_page sql = 'select id,name,sex,age,tel,addr,role from user where id >= \ ( select id from user order by id limit %s,1)limit %s' args = (start,per_page) conn = MySQLdb.connect(host=MYSQL_HOST, port=MYSQL_PORT, user=MYSQL_USER, passwd=MYSQL_PASSWORD, db=MYSQL_DB, charset='utf8mb4') cursor = conn.cursor() cursor.execute(sql, args) lines = cursor.fetchall() users = [] for line in lines: users.append({ 'id': line[0], 'name': line[1], 'sex': '男' if line[2] else '女', 'age': line[3], 'tel': line[4], 'addr': line[5], 'role': line[6] } ) cursor.close() conn.close() return users另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。