Discuss / SQL / 根据老师python的教程,做了简单的分页

根据老师python的教程,做了简单的分页

Topic source

根据老师教程做的一个网站

这数据库查询用的sqlalchemy

这是模型类创建的方法

def find_limit_with_user(self, start, count):
    result = dbsession.query(Article, Users.nickname).join(Users, Users.userid == Article.userid) \
        .filter(Article.hide == 0, Article.drafted == 0, Article.checked == 1) \
        .order_by(Article.articleid.desc()).limit(count).offset(start).all()
    dbsession.close()
    return result

这是控制层控制分页的地方

@index.route('/page/<int:page>')def pagenate(page):    if os.path.exists(f'./template/static/center-{page}.html'):         return render_template(f'/static/center-{page}.html')    start = (page - 1) * 10      article = Article()    result = article.find_limit_with_user(start, 10) # 进行分页查询    if article.get_total_count() % 10 == 0:        total = int(article.get_total_count() / 10)    else:        total = int(article.get_total_count() // 10 + 1)

输出的结果就是分页结果,十分清晰明了

希望可以帮到大家


  • 1

Reply