Skip to content

Commit 5d4c5ce

Browse files
author
rain
committed
分页
1 parent 3604ea6 commit 5d4c5ce

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

app/main/libs.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# encoding: utf-8
2+
from flask import abort
3+
from flask.ext.sqlalchemy import Pagination
4+
5+
6+
def do_paginate(query, page=1, per_page=20, error_out=True):
7+
if error_out and page < 1:
8+
abort(404)
9+
items = query.limit(per_page).offset((page - 1) * per_page).all()
10+
if not items and page != 1 and error_out:
11+
abort(404)
12+
if page == 1 and len(items) < per_page:
13+
total = len(items)
14+
else:
15+
total = query.order_by(None).count()
16+
return Pagination(query=None, page=page, per_page=per_page, total=total, items=items)

0 commit comments

Comments
 (0)