1、app.py文件

# 文章列表
@app.route(/list_city, methods=[GET, POST])
def list_city():
# 頁數通過reques方法取得page值
p = request.args.get(p, )
show_shouye_status = 0
if p == :
p = 1
else:
p = int(p)
if p > 1:
show_shouye_status = 1

limit_start = (int(p) - 1) * 10 # 起始

db = SQLManager()
citys = db.get_list(select * from city limit {0},10.format(limit_start))

sql = "select count(id) as total from city"
count = db.get_one(sql)[total]# 總記錄
total = int(math.ceil(count / 10.0))# 總頁數

dic = get_page(total, p)
datas = {
citys: citys,
p: int(p),
total: total,
show_shouye_status: show_shouye_status,
dic_list: dic
}

db.close()
return render_template(list.html, datas=datas)

2、config.py文件中的翻頁函數

def get_page(total,p):
show_page = 20 # 顯示的頁碼數
pageoffset = 4 # 偏移量
start = 1 #分頁條開始
end = total #分頁條結束

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)
#用於模版中循環
dic = range(start, end + 1)
return dic

3、模板文件調用

<ul class="pagination">
{% if datas.show_shouye_status==1%}
<li class=><a href={{ url_for(list_city, p=1) }}>首頁</a></li>
<li class=><a href={{ url_for(list_city, p=datas.p-1) }}>上一頁</a></li>
{%endif%}

{% for dic in datas.dic_list %}
{% if dic==datas.p%}
<li class="active"><a href="{{ url_for(list_city, p=dic) }}">{{dic}}</a></li>
{%else%}
<li><a href="{{ url_for(list_city,p=dic) }}">{{dic}}</a></li>
{%endif%}
{%endfor%}

{% if datas.p < datas.total%}
<li class=><a href={{ url_for(list_city,p=datas.p+1) }}>下一頁</a></li>
<li class=><a href={{ url_for(list_city,p=datas.total) }}>尾頁</a></li>
{%endif%}
共{{datas.total}}頁
</ul>

推薦閱讀:

相关文章