flask sqlalchemy 分页(二)

page.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!--这个是分页展示下面的页码-->
  
{%macro my_paginate(pagination,url)%}
  
<nav>
    <ul class="pagination">
  
        {%if pagination.has_prev%}
        <li class="page-item active"><a class="page-link" href="{{url_for(url,page=pagination.page-1)}}">上一页</a></li>
        {%else%}
        <li class="page-item disabled"><a class="page-link" href="#">上一页</a></li>
        {%endif%}
  
            {%for page in pagination.iter_pages()%}
                {%if page%}
                    <li class="page-item {%if page==pagination.page%}active{%endif%}"><a class="page-link" href="{{url_for(url,page=page)}}">{{page}}</a></li>
                {%else%}
                    <li class="page-item disabled"><a class="page-link" href="#">…</a></li>
                {%endif%}
  
            {%endfor%}
  
        {%if pagination.has_next%}
        <li class="page-item active"><a class="page-link" href="{{url_for(url,page=pagination.page+1)}}">下一页</a></li>
        {%else%}
        <li class="page-item disabled"><a class="page-link" href="#">下一页</a></li>
        {%endif%}
  
    </ul>
</nav>
{%endmacro%}

student.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生</title>
    <link href="{{url_for('static',filename='/css/style.css')}}" rel="stylesheet">
</head>
<body>
    <div align="center">
        <h2>用户信息</h2>
        <br><br>
         
         
        <tbody>
        <table>
            <colgroup>
                <col width="30%">
                <col width="40%">
                <col width="30%">
            </colgroup>
            <thead>
                <tr
                    <th>编号</th>
                    <th>姓名</th>
                    <th>成绩</th>
                </tr>
            </thead
             
            {% for stu in studentList %}
                <tr>
                    <td>{{ stu['id'] }}</td>
                    <td>{{ stu['name'] }}</td>
                    <td>{{ stu['score'] }}</td>
                </tr>
            {% endfor %}
        </table>
        </tbody>
    </div>
  
<!--     导入下面的页码-->
    {%import 'page.html' as pg%}
    {{pg.my_paginate(pagination,'query_stu')}}
<!--query_stu是对应的方法名称是什么,然后在点击页码时可以找到该方法,从而展示数据-->
  
</body>
</html>

style.css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* 分页容器样式 */
.pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 20px 0;
    list-style: none;
}
 
/* 页码按钮样式 */
.pagination a {
    padding: 8px 12px;
    margin: 0 4px;
    text-decoration: none;
    color: #333;
    border: 1px solid #ddd;
    border-radius: 4px;
}
 
/* 当前页码样式 */
.pagination a.active {
    background-color: #007bff;
    color: #fff;
    border-color: #007bff;
}
 
/* 鼠标悬停在页码按钮上的样式 */
.pagination a:hover {
    background-color: #f5f5f5;
}
 
table {
    border-collapse: collapse;
    width: 60%;
    border: 1px solid #ddd;
    font-size: 14px;
}
 
th, td {
    border: 1px solid #ddd;
    padding: 8px;
    text-align: center;
 
}
 
th {
    background-color: #000/*#4CAF50*/;
    color: white;
}
 
tr:nth-child(even) {
    background-color: #f2f2f2;
}
 
tr:hover {
    background-color: #ddd;
}

  

 

database.py

1
2
3
from flask_sqlalchemy import SQLAlchemy
 
db = SQLAlchemy()

models.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from database import db
from config import app
class StudentMore(db.Model):
    __tablename__= 'Students'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(108), nullable=False)
    score = db.Column(db.Integer, nullable=False)
     
    def __repr__(self):
        return 'StudentMore  %r' % self.name
     
     
with app.app_context():
    db.create_all()

app.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from flask import Flask,render_template
from flask import redirect
from flask import url_for
from database import db
from config import app
from models import StudentMore
  
@app.route('/stu/query/<int:page>', methods=['GET'])
def query_stu(page=None):
    if not page:
        page = 1
    students = StudentMore.query.paginate(page=page,per_page=5)
    # page是第几页,per_page是将每5个一页
  
    db.session.commit()
  
    # students.items是分页展示的数据
    return render_template("student.html", studentList=students.items,  pagination=students)
  
  
@app.route('/')
def index():
    return redirect(url_for("query_stu",page=1))
  
  
if __name__=="__main__":
    app.run(debug=True)

  

  

  

  

  

posted @   tec2019  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2023-10-10 vba发送电子邮件利用outlook
点击右上角即可分享
微信分享提示