提交表单后,可以使用干净的方法还原为?page = 1(使用flask-paginate)

我的模板中有一个表单,该表单在提交时会为我的数据库查询提供搜索过滤器。否则,查询将返回所有文档。我正在使用烧瓶分页浏览结果。

我的错误是,如果用户提交表单时位于第2页上,则即使返回的文档数不等于2,该页面也会使用新查询的结果重新加载第2页。超过1页。在这种情况下,加载的页面只是空白,没有分页链接可导航回到页面1。到目前为止,我已经通过以下方式解决了这一问题:将表格形式的flask-paginate的'page'变量的值更改为等于1已提交。现在似乎从第1页加载了结果,当我打印(page)时,它正确打印了“ 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"},]
        )
    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,pagination=pagination,form=form,title="Things to do",)
iCMS 回答:提交表单后,可以使用干净的方法还原为?page = 1(使用flask-paginate)

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2004771.html

大家都在问