我的模板中有一个表单,在提交时为我的数据库查询提供搜索过滤器。否则,查询将返回所有文档。我正在使用flask paginate导航结果
我的错误是,如果用户提交表单时在第2页,则该页会重新加载第2页,并显示新查询的结果,即使返回的文档数不超过1页。在这种情况下,加载的页面为空,没有分页链接可导航回第1页。到目前为止,我已经通过在提交表单时将flask paginate的“page”变量的值更改为等于1半解决了这个问题。现在它似乎加载了第1页的结果,当我打印(第页)时,它正确地打印了“1”。但是,url中的查询字符串仍然显示?page=2。因此,我正在寻找一个更清洁的解决方案?还是清除查询字符串的方法
@app.route("/thingstodo/<city>", methods=["GET", "POST"])
def suggestion_list(city):
form = FilterResultsForm()
cities = mongo.db.cities
page, per_page, offset = get_page_args(
page_parameter="page", per_page_parameter="per_page"
)
print(page)
if form.validate_on_submit():
page = 1
per_page = 3
offset = (page - 1) * per_page
query = ""
if form.validate_on_submit() or "filters" in session:
filters = (
form.category.data if form.category.data else session["filters"]
)
session["filters"] = filters
array = []
for filter in filters:
newdict = {}
newdict["thingsToDo.category"] = filter
array.append(newdict)
query = cities.aggregate(
[
{"$unwind": "$thingsToDo"},
{"$match": {"location": city, "$or": array}},
{
"$lookup": {
"from": "users",
"localField": "thingsToDo.author",
"foreignField": "username",
"as": "user_profile",
}
},
{"$unwind": "$user_profile"},
{
"$project": {
"suggestion": "$thingsToDo.suggestion",
"cost": "$thingsToDo.cost",
"category": "$thingsToDo.category",
"url": "$thingsToDo.url",
"comment": "$thingsToDo.comment",
"author": "$user_profile.username",
"profile": "$user_profile.picture",
}
},
]
)
else:
query = cities.aggregate(
[
{"$match": {"location": city}},
{"$unwind": "$thingsToDo"},
{
"$lookup": {
"from": "users",
"localField": "thingsToDo.author",
"foreignField": "username",
"as": "user_profile",
}
},
{"$unwind": "$user_profile"},
{
"$project": {
"suggestion": "$thingsToDo.suggestion",
"cost": "$thingsToDo.cost",
"category": "$thingsToDo.category",
"url": "$thingsToDo.url",
"comment": "$thingsToDo.comment",
"author": "$user_profile.username",
"profile": "$user_profile.picture",
}
},
]
)
results = list(query)
total = len(results)
suggestions = results[offset: offset + per_page]
pagination = Pagination(
page=page, per_page=per_page, total=total, css_framework="bootstrap4"
)
return render_template(
"thingstodo.html",
city=city,
things=suggestions,
page=page,
per_page=per_page,
pagination=pagination,
form=form,
title="Things to do",
)
目前没有回答
相关问题 更多 >
编程相关推荐