import math from flask import Blueprint, jsonify, request from flask import render_template from ..model import School # 导入上级模块 s = Blueprint('school', __name__, url_prefix='/ss') def Pagination(page, total): """ Pagination 用于封装分页参数到字典变量中,并返回到前台 :param page: 传入参数,页码 :param total: 数据总量 :return: """ page_obj = { "prev_page": page - 1, "next_page": page + 1, "current_page": 0, "total_page": 0, "max_page": 0, "page_size": 20, "total": total, "offset": 0, "page_range": None } page_obj["total_page"] = math.ceil(total / page_obj["page_size"]) page_obj["max_page"] = page_obj["total_page"] if page <= 1: page = 1 page_obj["prev_page"] = 1 if page >= page_obj["max_page"]: page = page_obj["max_page"] page_obj["next_page"] = page_obj["max_page"] page_obj["current_page"] = page if total == 0: page_obj["offset"] = 0 else: page_obj["offset"] = (page_obj["current_page"] - 1) * page_obj["page_size"] page_range = [] for i in range(1, page_obj["max_page"] + 1): page_range.append(i) page_obj["page_range"] = page_range return page_obj @s.route('/list') def list_school(): # schools = School.query.all() page = int(request.args.get("page", 1)) # 获取页码 query = School.query total = query.count() # 获取数据总量 pagination = Pagination(page, total) # 获取分页对象详细参数 if total != 0: data_list = query.offset(pagination["offset"]).limit(pagination["page_size"]).all() else: data_list = [] pagination["data_list"] = data_list return render_template('school/index.html', pagination=pagination)