提交 047a07fa 编写于 作者: 梦想橡皮擦's avatar 梦想橡皮擦 💬

首次分页实现

上级 db4d3795
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from .config import BaseConfig # 导入配置文件
app = Flask(__name__)
app.config.from_object(BaseConfig) # 启用配置
db = SQLAlchemy()
db.init_app(app) # 初始化数据库
# 导入路由配置
from app import routes
from app import general
\ No newline at end of file
from app import general
from .school.index import *
app.register_blueprint(s)
class BaseConfig(object):
DIALECT = 'mysql'
DRIVER = 'pymysql'
USERNAME = 'root'
PASSWORD = 'root'
HOST = '127.0.0.1'
PORT = '3306'
DATABASE = 'playground'
SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT, DRIVER, USERNAME, PASSWORD, HOST,
PORT, DATABASE)
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_ECHO = True
from app import db
class EntityBase(object):
def to_json(self):
fields = self.__dict__
if "_sa_instance_state" in fields:
del fields["_sa_instance_state"]
return fields
class School(db.Model, EntityBase):
"""
表名,字段名
"""
__tablename__ = "school_list"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
province = db.Column(db.String(255))
city = db.Column(db.String(255))
feature = db.Column(db.String(255))
hotValue = db.Column(db.String(255))
pic = db.Column(db.String(255))
category = db.Column(db.String(255))
batchTimes = db.Column(db.String(255))
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)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>爬虫训练场,版权:梦想橡皮擦</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css">
<link rel="stylesheet" href="{{url_for('static',filename='css/base.css')}}">
</head>
<body>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<div class="container-fluid">
<a href="/" class="navbar-brand">
<img src="/static/images/sucai.png" style="width:2rem;" class="rounded-pill" alt=""/>爬虫训练场</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<ul class="navbar-nav">
<li class="nav-item">
<a href="#" class="nav-link">案例场</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">爬虫博客</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">社群</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col">
<span class="text-dark float-end align-middle" style="line-height: 40px;">合计 {{pagination.total}} 条数据</span>
<ul class="pagination float-end">
<li class="page-item">
<a class="page-link" href="{{url_for('school.list_school')}}?page={{pagination.prev_page }}">上一页</a>
</li>
<li class="page-item"><a class="page-link"
href="{{ url_for('school.list_school') }}?page={{ pagination.next_page }}">下一页</a>
</li>
</ul>
</div>
</div>
</div>
<div class="container-fluid text-center text-muted ">
<hr>
<p>爬虫训练场 Copyright © 梦想橡皮擦</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
\ No newline at end of file
......@@ -2,5 +2,7 @@ from app import app
# app.jinja_env.auto_reload = True
app.config['TEMPLATES_AUTO_RELOAD'] = True
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8888)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册