index.py 1.8 KB
Newer Older
梦想橡皮擦's avatar
梦想橡皮擦 已提交
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 58 59 60 61 62 63 64 65 66 67 68 69 70
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)