本次使用的是Flask框架
后端代码
@app.route('/movie',methods=['GET','POST'])
def movie(): # 电影
page = request.args.get('page',1)
conn = sqlite3.connect('movie.db')
cur = conn.cursor()
# 获取数据总条数
sql = 'SELECT count(id) FROM top250'
data = cur.execute(sql)
totals = data.fetchone()[0]
# 分页
page_info = pagination(int(page), totals)
dataList = []
if totals != 0:
sql1 = 'SELECT * FROM top250 limit %d,%d' % (page_info['offset'], page_info['page_size'])
data1 = cur.execute(sql1)
for row in data1:
dataList.append(row)
cur.close()
conn.close()
return render_template('movie.html', dataList=dataList,page_info=page_info)
分页函数
# 分页
# page_num 页码
# totals 数据总条数
def pagination(page_num, totals):
ret = {
'prev_page': page_num - 1, #上一页
'next_page': page_num + 1, #下一页
'current_page': 0, #当前页码
'total_pages': 0, #总页数/最大页
'page_size':10, #每页数量
'totals': totals, #数据总条数
'offset':0, #偏移量
'page_range':None #页码范围
}
ret['total_pages'] = math.ceil(totals / ret['page_size'])
if page_num <= 1:
page_num = 1
ret['prev_page'] = 1
if page_num >= ret['total_pages']:
page_num = ret['total_pages']
ret['next_page'] = ret['total_pages']
ret['current_page'] = page_num
if totals == 0:
ret['offset'] = 0
else:
ret['offset'] = (ret['current_page'] - 1) * ret['page_size']
page_range = []
for i in range(1,ret['total_pages'] + 1):
page_range.append(i)
ret['page_range'] = page_range
return ret
前端代码
<!-- 分页 -->
<nav aria-label="Page navigation example">
<ul class="pagination mb-5 justify-content-center">
<li class="page-item"><a class="page-link" href="{{ url_for('movie') }}?page=1">首页</a></li>
<li class="page-item">
<a class="page-link" href="{{ url_for('movie') }}?page={{ page_info.prev_page }}"><span>«</span></a>
</li>
{% if page_info.total_pages < 8 %}
{% for item in page_info.page_range %}
<li class="page-item {% if page_info.current_page == item %}active{% endif %}"><a class="page-link" href="{{ url_for('movie') }}?page={{ item }}">{{ item }}</a></li>
{% endfor %}
{% else %}
{% if page_info.current_page <= 5 %}
{% for item in range(1,7) %}
<li class="page-item {% if page_info.current_page == item %}active{% endif %}"><a class="page-link" href="{{ url_for('movie') }}?page={{ item }}">{{ item }}</a></li>
{% endfor %}
<li class="page-item"><a class="page-link" href="#">...</a></li>
{% elif page_info.current_page >= page_info.total_pages - 4 %}
<li class="page-item"><a class="page-link" href="#">...</a></li>
{% for item in range(page_info.total_pages - 5,page_info.total_pages + 1) %}
<li class="page-item {% if page_info.current_page == item %}active{% endif %}"><a class="page-link" href="{{ url_for('movie') }}?page={{ item }}">{{ item }}</a></li>
{% endfor %}
{% else %}
<li class="page-item"><a class="page-link" href="#">...</a></li>
{% for item in range(page_info.current_page - 2,page_info.current_page + 3) %}
<li class="page-item {% if page_info.current_page == item %}active{% endif %}"><a class="page-link" href="{{ url_for('movie') }}?page={{ item }}">{{ item }}</a></li>
{% endfor %}
<li class="page-item"><a class="page-link" href="#">...</a></li>
{% endif %}
{% endif %}
<li class="page-item">
<a class="page-link" href="{{ url_for('movie') }}?page={{ page_info.next_page }}"><span >»</span></a>
</li>
<li class="page-item"><a class="page-link" href="{{ url_for('movie') }}?page={{ page_info.total_pages }}">尾页</a></li>
</ul>
</nav>
<!-- 分页end -->
最终样式