没有SQLAlchemy的Flask分页



希望在不使用SQLAlchemy的情况下对数据集进行分页。我正在使用查询从数据库中获取数据。

def data():
cursor.execute("""select * from table_data""")
records = cursor.fetchall()
return reneder_template('data.html')

和我渲染这个结果在@app。路由方法。

分页可以使用SQLAlchemy中的分页和查询方法来完成,但由于我直接从数据库中获取数据,因此建议将对分页有帮助。

下面的方法可以用来在Flaks中分页,而不需要使用SQLAlchemy,

def data():
page = request.args.get(get_page_parameter(), type=int, default=1)
limit=20
offset = page*limit - limit
cursor = connection.cursor()
cursor.execute("""select * from user_listing
table_data""")
result = cursor.fetchall()
total = len(result)
print(total)
cursor.execute("""select * from table_data
limit %s offset %s""", (limit, offset))
data = cursor.fetchall()
pagination = Pagination(page=page, page_per=limit, total=total)
return render_template('data.html', pagination=pagination, data = data)

参考- https://pythonhosted.org/Flask-paginate/

最新更新