如何解决使用flask-paginate对结果进行分页时,防止多个数据库提取
我有一个包含表单的Flask应用程序。基于表单数据,我正在调用数据库以获取数据。可能会有很多结果,所以我在使用flask-paginate时要确保没有太多滚动。我遇到的问题是,每次移至下一页时,我都在与数据库建立新连接并再次运行查询。我使用的是NoSQL数据库(DynamoDB),而不是sql,因此无法使用sqlalchemy分页功能。
(编辑:在评论中提醒我,dynamo分页不同于sql分页,并且为澄清起见,我知道其中的区别,因此我不打算使用它)。
我的问题:每当用户从一页移到下一页时,防止从数据库中获取数据的最佳方法是什么?重新加载页面后如何保存结果?
这是一个代码段
@views.route('/index',methods=["POST","GET"])
def index():
page,per_page,offset = get_page_args(page_parameter='page',per_page_parameter='per_page')
db = DynamoDb() # constructor makes a connection to the database,which is happening every time the user goes to the next page
my_form = MyForm() # MyForm is a child of FlaskForm
if my_form.validate_on_submit():
return redirect(url_for("views.index",arg_to_pass=my_form.field.data))
elif request.method == "GET" and request.args.get("arg_to_pass") is not None:
# db.get_data() makes a request to dynamodb and then formats the result in a dictionary. It's possible that a large number of results will be returned
# this transaction on dynamodb is also happening every time the user goes to the next page
# How can I just fetch the data once and persist it when the user goes to the next page?
large_dict = db.getdata(arg_to_pass)
pagination_users = list(large_dict.keys())[offset: offset + per_page]
pagination = Pagination(page=page,per_page=per_page,total=total,css_framework="bootstrap4")
return render_template('index.html',my_form=my_form,large_dict=large_dict
pagination_users=pagination_users
page=page,pagination=pagination)
return render_template("index.html",my_form=my_form)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。