【问题标题】:Python flask - redirect to same page with query resultsPython烧瓶 ​​- 使用查询结果重定向到同一页面
【发布时间】:2020-12-12 21:47:18
【问题描述】:

我已经查看了许多其他答案,但无法弄清楚它们与我的问题有何关系。我对 Python 和 Flask 比较陌生,但很想学习。

上下文是我在主页上添加父亲和/或母亲(以任何顺序)并重定向到 /family/ 页面。显示该页面后,我可以添加另一个父级,然后继续添加子级。我可以根据需要添加任意数量的孩子,每次添加孩子的名字时,我都希望它显示在列表中,并在添加每个孩子后出现一对空白字段和​​一个添加按钮(为下一个孩子做好准备)。其中大部分工作正常,但我试图防止我提交的最后一个孩子的名字/姓氏在刷新页面时被重新提交。

这是我的 Python 代码的一部分:

@app.route("/family/<parentsid>", methods=["GET", "POST"])
def show_family(parentsid):

    form = AddIndividual()

    if request.method == "POST":

        if request.form.get("addfather") == "Add":
            add_father(form)

            return redirect(url_for("show_family", parentsid=session["partners.id"]))

        if request.form.get("addmother") == "Add":
            add_mother(form)

            return redirect(url_for("show_family", parentsid=session["partners.id"]))

        if request.form.get("addchild") == "Add":
            child_forenames = form.child_forenames.data
            child_surname = form.child_surname.data
            child_fullname = fullname(child_forenames, child_surname)

            new_child = Individual(child_surname, child_fullname, child_forenames)
            db.session.add(new_child)

            db.session.commit()
            db.session.flush()

            session["child.id"] = new_child.id

            link_child(individual_id=session["child.id"], parents_id=session["partners.id"])

            childlist = db.session.query(Individual.fullname).join(FamilyLink).filter(Parents.id == parentsid).all()
            children = [c[0] for c in childlist]

            session["children"] = children

            form.child_forenames.data = ""

            return render_template("home.html", form=form, father_fullname=father_fullname,
                                   mother_fullname=mother_fullname, children=children) # This is probably the problem!

            # return redirect(url_for("show_family", parentsid=session["partners.id"]))  This is the other line I've tried (see below)

    return render_template("home.html", form=form, father_fullname=father_fullname, mother_fullname=mother_fullname)

HTML模板的相关部分是:

            <form method="POST" class="col-md-12">
                {{form.hidden_tag()}}
                    <div class="row">
                        <div class="col col-sm-6">
                            <h4>Children</h4>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col col-sm-6">
                            {% if children %}
                            <ul class="list-group">
                                {% for child in children %}
                                <li class="list-group-item"><h3>{{ child }}</h3></li>
                                {% endfor %}
                            </ul>
                            <br>
                            {% endif %}
                            <div class="input-group">
                                {{ form.child_forenames() }}
                                {{ form.child_surname() }}
                                <input type="submit" class="btn btn-primary" value="Add" name="addchild"/>
                            </div>
                        </div>
                    </div>
            </form>

从其他答案来看,我确信答案是使用重定向而不是 render_template 并可能使用 GET(以及我需要从表单提交数据的 POST)。

如果我使用 return redirect(url_for("show_family", parentsid=session["partners.id"])) 而不是 render_template ,表单不会重新提交任何很棒的东西,但它根本不会显示子列表(即使它可以很好地保存数据)。

有人可以帮忙吗?

谢谢。

【问题讨论】:

    标签: python python-3.x flask flask-sqlalchemy


    【解决方案1】:

    重定向在页面上执行get。它在我们函数的最后一行使用了 render_template。您不会在那里传递孩子,因此页面不会显示他们。

    【讨论】:

      猜你喜欢
      • 2018-02-18
      • 2017-09-26
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多