Saturday, 15 January 2011

python - Pagination in Flask using MySql -



python - Pagination in Flask using MySql -

i searched lot it. articles include sqlalchemy , none of them deal mysql. working flask , have database in mysql , need display info in pages. 1000 images, per page 10 100 pages. in mysql can pagination help of limit. , routes can be:

@app.route('/images', defaults={'page':1}) @app.route('/images/page/<int:page>')

i need inquire needed pagination? or forgetting of import here? , mysql syntax be:

db = mysql.connect('localhost', 'root', 'password', 'img') cursor = db.cursor() cursor.execute('select id,title,img image order rand() limit 20 offset 0;') info = list(cursor.fetchall())

to first 20 results how next according pageno.? flask-paginate library works tsqlalchemy.

try

@app.route('/images', defaults={'page':1}) @app.route('/images/page/<int:page>') def abc(page): perpage=20 startat=page*perpage db = mysql.connect('localhost', 'root', 'password', 'img') cursor = db.cursor() cursor.execute('select id,title,img image limit %s, %s;', (startat,perpage)) info = list(cursor.fetchall())

may help.

python mysql pagination flask

No comments:

Post a Comment