分页
from flask import *
from flask_sqlalchemy import SQLAlchemy
app = Flask(name)
app.config[‘SQLALCHEMY_DATABASE_URI’] = ‘mysql+pymysql://root:12345678@127.0.0.1:3306/KaoShi’
db = SQLAlchemy(app)
用户表
class User(db.Model):
tablename = ‘user’
id = db.Column(db.Integer, primary_key=True)
user_name = db.Column(db.String(200))
pwd = db.Column(db.String(200))
tel = db.Column(db.String(20))
@app.route(’/’)
def index():
word = request.args.get('word', '')
current_page = request.args.get('page', 1, type=int) # 当前页
per_page = 2 # 每页条数
result = User.query.filter(User.user_name.like('%'+ word +'%')).paginate(current_page, per_page)
# 组织数据
d = {}
d['user_list'] = result.items # 数据集
d['current_page'] = result.page # 当前页
d['total_page'] = result.pages # 总页数
d['word'] = word
return render_template('paginate.html', data=d)
if name == ‘main’:
app.run()