@api.route('/') def post_page(): start_index = request.args.get('start', 1, type=int) # 获取起始索引,默认为1 end_index = request.args.get('end', 5, type=int) # 获取结束索引,默认为10 # 计算偏移量和每页显示数量 per_page = end_index - start_index + 1 offset = (start_index - 1) # 查询数据库获取帖子数据 posts = Post.query.order_by(Post.date_posted.desc()).offset(offset).limit(per_page).all() # 将帖子数据转换为 JSON 格式 posts_json = [{ 'title': post.title, 'content': post.content, 'date_posted': post.date_posted.strftime('%Y-%m-%d %H:%M:%S'), 'author_id': post.user_id # 假设这是作者的用户 ID } for post in posts] # 返回 JSON 数据 return jsonify(posts=posts_json)
flask--分页项目应用
最新推荐文章于 2024-09-14 10:45:13 发布