Python+bootstrap实现分页

本次使用的是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>&laquo;</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 >&raquo;</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 -->

最终样式

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值