flask_sqlalchemy 分页(一)
database.py
1 2 3 | from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() |
config.py
1 2 3 4 5 6 7 8 9 10 11 12 13 | import os from database import db from flask import Flask basedir = os.path.abspath(os.path.dirname(__name__)) app = Flask(__name__) #Dabase configuration app.config[ 'SQLALCHEMY_DATABASE_URI' ] = os.environ.get( 'DATABASE_URI' ) or 'sqlite:///' + os.path.join(basedir, 'app.db' ) app.config[ 'SQLALCHEMY_TRACK_MODIFICATIONS' ] = False db.init_app(app) |
models.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 ) |
templates/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 47 48 | <!DOCTYPE html> <html lang = "en" > <head> <meta charset = "UTF-8" > <title>学生< / title> <style> ul { margin: auto; width: 241px ; } ul li { float : left; margin - right: 16px ; list - style: none; } ul li a { text - decoration: none; } ul li a:hover { color: orange; } .active a{ color: red ; } .disabled a { color: gray ; } < / style> < / head> <body> <div align = "center" > <h2>用户信息< / h2> <br><br> { % for stu in studentList % } 编号:{{ stu[ 'id' ] }},姓名:{{ stu[ 'name' ] }},成绩:{{ stu[ 'score' ] }} <br><br> { % endfor % } < / div> <! - - 导入下面的页码 - - > { % import 'page.html' as pg % } {{pg.my_paginate(pagination, 'query_stu' )}} <! - - query_stu是对应的方法名称是什么,然后在点击页码时可以找到该方法,从而展示数据 - - > < / body> < / html> |
分类:
编程
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)