如何将分页作为jsonify()返回到前端



这是我的管理书籍列表,我想添加分页。我如何添加分页并在jsonify((中返回到vue作为前端?

@phonebook.route('/admin/book/list')
@admin_authorize
def admin_books(user): 
respone_body = dict()
respone_body['status'] = "success"
respone_body['code'] = 0
admin_book = admin_book(user.id, sort_by_id_desc=True)
respone_body['data']={
'books': admin_book,
}
return jsonify(respone_body)

def admin_book(user_id, sort_by_id_desc=True):
admin_books = []
for book in Book.query.all():
admin_books.append({
"id": book.id,
"name": book.name,
"date": book.date,
})
return sorted(admin_books, key=lambda dict: dict['id'], reverse=sort_by_id_desc)

感谢您提前提供帮助!

我得到了答案,这就是我在jsonify((中返回Flask Restful Api分页的方式。Miguel Grinberg的Flask Book帮助我理解了的概念

@phonebook.route('/admin/book/list')
@admin_authorize
def admin_books(user): 
respone_body = dict()
respone_body['status'] = "success"
respone_body['code'] = 0
admin_book, pagination = admin_book(user.id, sort_by_id_desc=True)
respone_body['data']={
'books': admin_book,
'page': pagination.page,
'per_page': pagination.per_page,
'total_page': pagination.pages,
}
return jsonify(respone_body)

def admin_book(user_id, sort_by_id_desc=True):
admin_books = [] 
page = request.args.get('page',1,type=int) 
per_page = current_app.config['PER_PAGE']
pagination = Book.query.paginate(page, per_page)
books = pagination.items
for book in books:
admin_books.append({
"id": book.id,
"name": book.name,
"date": book.date,
})
return sorted(admin_books, key=lambda dict: dict['id'], reverse=sort_by_id_desc), pagination

我希望这能帮助以后像我做一样的人

相关内容

  • 没有找到相关文章

最新更新