flask_sqlalchemy中数据(slice)切片的问题

在flask中研究分页问题的时候发现使用query.slice(start,end)数据没办法分割

from models.Article import Article
from flask_paginate import Pagination,get_page_parameter


#主页
index_route=Blueprint('index_route',__name__)


@index_route.route('/')
@index_route.route('/index/')
@index_route.route('/home/')
def index():
   page_content = 10
   total = Article.query.count()
   page = request.args.get(get_page_parameter(),type=int,default=1)
   start = (page-1)*page_content
   end = start +page_content
   pagination = Pagination(bs_version=3,page = page,total = total)
   articles =Article.query.order_by(Article.create_time.desc()).slice(start,end)
   
   
   return render_template('index.html',articles = articles,pagination= pagination)

在使用上面的代码时候,返回全部的数据,没有达到理想的效果。

在查找资料的过程中(https://blog.csdn.net/qq_42239520/article/details/80378095),它使用session来查询数据,博主抱着试试的想法使用db.session来查找数据。

from models.Article import Article
from flask_paginate import Pagination,get_page_parameter
from models import db

#主页
index_route=Blueprint('index_route',__name__)


@index_route.route('/')
@index_route.route('/index/')
@index_route.route('/home/')
def index():
   page_content = 10
   total = Article.query.count()
   page = request.args.get(get_page_parameter(),type=int,default=1)
   start = (page-1)*page_content
   end = start +page_content
   pagination = Pagination(bs_version=3,page = page,total = total)
   articles = db.session.query(Article).order_by(Article.create_time.desc()).slice(start,end)
   
   
   return render_template('index.html',articles = articles,pagination= pagination)

成功把数据切割

发表评论

电子邮件地址不会被公开。 必填项已用*标注