新聞中心
在web開發(fā)中,分頁是必不可少的功能,F(xiàn)lask實現(xiàn)展示內(nèi)容的分頁也非常簡單,這里通過實例來學(xué)習(xí)一下Flask如何為網(wǎng)站分頁。

成都創(chuàng)新互聯(lián)公司成立十余年來,這條路我們正越走越好,積累了技術(shù)與客戶資源,形成了良好的口碑。為客戶提供成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、網(wǎng)站策劃、網(wǎng)頁設(shè)計、域名注冊、網(wǎng)絡(luò)營銷、VI設(shè)計、網(wǎng)站改版、漏洞修補等服務(wù)。網(wǎng)站是否美觀、功能強大、用戶體驗好、性價比高、打開快等等,這些對于網(wǎng)站建設(shè)都非常重要,成都創(chuàng)新互聯(lián)公司通過對建站技術(shù)性的掌握、對創(chuàng)意設(shè)計的研究為客戶提供一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進步。
首先,自定義一個分頁工具類page_utils:
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 self.total_count = total_count self.per_page_count = per_page_count max_page_num, div = divmod(total_count, per_page_count) if div: max_page_num += 1 self.max_page_num = max_page_num self.max_pager_count = max_pager_count self.half_max_pager_count = int((max_pager_count - 1) / 2) self.base_url = base_url 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): if self.max_page_num <= self.max_pager_count: pager_start = 1 pager_end = self.max_page_num # 如果總頁數(shù) > 11 else: if self.current_page <= self.half_max_pager_count: pager_start = 1 pager_end = self.max_pager_count else: 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 = [] self.params['page'] = 1 first_page = '
自定義方法中的參數(shù):
current_page——表示當(dāng)前頁。
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:
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 {% for foo in index_list %}
- {{ foo }}:這是列表內(nèi)容~~
{% endfor %}
這樣一個分頁的效果就做好了,我們查看效果,如下圖:
網(wǎng)站標(biāo)題:創(chuàng)新互聯(lián)Python教程:flask里如何實現(xiàn)分頁功能
本文路徑:http://www.5511xx.com/article/cccijje.html


咨詢
建站咨詢
