日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python flask分页_flask 分页

發(fā)布時間:2025/3/15 python 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python flask分页_flask 分页 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

#!usr/bin/env python

# -*- coding:utf-8 -*-

from urllib import urlencode

class Pagination(object):

"""

自定義分頁

"""

def __init__(self, current_page, total_count, base_url, params, per_page_count=10, max_pager_count=11):

try:

current_page = int(current_page)

except Exception as e:

current_page = 1

if current_page <=0:

current_page = 1

self.current_page = current_page

# 數(shù)據(jù)總條數(shù)

self.total_count = total_count

# 每頁顯示10條數(shù)據(jù)

self.per_page_count = per_page_count

# 頁面上應(yīng)該顯示的最大頁碼

max_page_num, div = divmod(total_count, per_page_count)

if div:

max_page_num += 1

self.max_page_num = max_page_num

# 頁面上默認顯示11個頁碼(當前頁在中間)

self.max_pager_count = max_pager_count

self.half_max_pager_count = int((max_pager_count - 1) / 2)

# URL前綴

self.base_url = base_url

# request.GET

import copy

params = copy.deepcopy(params)

get_dict = params.to_dict()

self.params = get_dict

@property

def start(self):

return (self.current_page - 1) * self.per_page_count

@property

def end(self):

return self.current_page * self.per_page_count

def page_html(self):

# 如果總頁數(shù) <= 11

if self.max_page_num <= self.max_pager_count:

pager_start = 1

pager_end = self.max_page_num

# 如果總頁數(shù) > 11

else:

# 如果當前頁 <= 5

if self.current_page <= self.half_max_pager_count:

pager_start = 1

pager_end = self.max_pager_count

else:

# 當前頁 + 5 > 總頁碼

if (self.current_page + self.half_max_pager_count) > self.max_page_num:

pager_end = self.max_page_num

pager_start = self.max_page_num - self.max_pager_count + 1 #倒這數(shù)11個

else:

pager_start = self.current_page - self.half_max_pager_count

pager_end = self.current_page + self.half_max_pager_count

page_html_list = []

# {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]}

# 首頁

self.params['page'] = 1

first_page = '

首頁'.decode("utf-8") % (self.base_url,urlencode(self.params),)

page_html_list.append(first_page)

# 上一頁

self.params["page"] = self.current_page - 1

if self.params["page"] < 1:

pervious_page = '

上一頁'.decode("utf-8") % (self.base_url, urlencode(self.params))

else:

pervious_page = '

上一頁'.decode("utf-8") % ( self.base_url, urlencode(self.params))

page_html_list.append(pervious_page)

# 中間頁碼

for i in range(pager_start, pager_end + 1):

self.params['page'] = i

if i == self.current_page:

temp = '

%s' % (self.base_url,urlencode(self.params), i,)

else:

temp = '

%s' % (self.base_url,urlencode(self.params), i,)

page_html_list.append(temp)

# 下一頁

self.params["page"] = self.current_page + 1

if self.params["page"] > self.max_page_num:

self.params["page"] = self.current_page

next_page = '

下一頁'.decode("utf-8") % (self.base_url, urlencode(self.params))

else:

next_page = '

下一頁'.decode("utf-8") % (self.base_url, urlencode(self.params))

page_html_list.append(next_page)

# 尾頁

self.params['page'] = self.max_page_num

last_page = '

尾頁'.decode("utf-8") % (self.base_url, urlencode(self.params),)

page_html_list.append(last_page)

return ''.join(page_html_list)

自定義方法中的參數(shù):

current_page——表示當前頁。

total_count——表示數(shù)據(jù)總條數(shù)。

base_url——表示分頁URL前綴,請求的前綴獲取可以通過Flask的request.path方法,無需自己指定。

例如:我們的路由方法為@app.route('/test'),request.path方法即可獲取/test。

params——表示請求傳入的數(shù)據(jù),params可以通過request.args動態(tài)獲取。

例如:我們鏈接點擊為:http://localhost:5000/test?page=10,此時request.args獲取數(shù)據(jù)為ImmutableMultiDict([('page', u'10')])

per_page_count——指定每頁顯示數(shù)。

max_pager_count——指定頁面最大顯示頁碼

接著,我們使用一個測試方法來使用這個工具類,達到分頁效果,test.py:

#!usr/bin/env python

# -*- coding:utf-8 -*-

from flask import Flask, render_template, request

from page_utils import Pagination

app = Flask(__name__)

@app.route('/test')

def test():

li = []

for i in range(1, 100):

li.append(i)

pager_obj = Pagination(request.args.get("page", 1), len(li), request.path, request.args, per_page_count=10)

print(request.path)

print(request.args)

index_list = li[pager_obj.start:pager_obj.end]

html = pager_obj.page_html()

return render_template("obj/test.html", index_list=index_list, html=html)

if __name__ == '__main__':

app.run(debug=True)

在上面的程序中,li為我們要分頁的對象,數(shù)組list,我們獲取到這個list之后,把他用工具類中的起止方法包起來。

傳遞數(shù)據(jù)用包裝后的list,這樣就達到了需要哪一段數(shù)據(jù)我們傳遞哪一段的效果,包裝的方法:index_list = li[pager_obj.start:pager_obj.end]

我們用一個HTML頁面去顯示它,分頁樣式不是重點,我們這里直接引入bootstrap封裝好的分頁效果,代碼如下:

Title

.container{

margin-top: 20px;

}

{% for foo in index_list %}

{{ foo }}:這是列表內(nèi)容~~

{% endfor %}

{{ html|safe }}

總結(jié)

以上是生活随笔為你收集整理的python flask分页_flask 分页的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。