【问题标题】:Flask Passing Arguments From One Html page to anotherFlask 将参数从一个 Html 页面传递到另一个
【发布时间】:2017-04-30 22:23:28
【问题描述】:

我是烧瓶、HTML、Jinja 的新手,我在将值从一个模板传递到另一个模板时遇到了麻烦。我查看了关于 SO 的许多其他页面,但没有人遇到完全相同的问题。我有一个带有下拉选择菜单和提交按钮的页面,一旦用户做出选择,它就会重定向用户。我的问题是我不知道如何将该选择传递给下一页的表单。以下是我的 HTML 表单代码:

 <form method="POST" action="search">
            <div class="form-group" align="center">
                 <select class="vertical-menu">
                    {% for friend in strFriendList %}
                        <option name="friendToPay" value="{{ friend }}">{{ friend }}</option>
                    {% endfor %}
                </select>

                <a href="{{ url_for('payment') }}"  class="btn-primary"> Submit</a>
            </div>
 </form>

以下是我的搜索和支付功能:

    @app.route("/search")
def search():
  if "email" not in session:
    return redirect(url_for("login"))
  else:
    friendslist1 = Friendship.query.filter_by(username=session["username"]).all()
    friendslist2 = Friendship.query.filter_by(friendUserName=session["username"])
    strFriendList = [""]
    for friend in friendslist1:
      strFriendList.append(friend.friendUserName)
    for friend in friendslist2:
      strFriendList.append(str(friend.username))
    form = SelectFriendForm()
    return render_template("search.html",strFriendList=strFriendList,form=form)

@app.route("/payment",methods=['GET', 'POST'])
def payment(personToPay):
    form = PaymentForm()
    if request.method == "POST":
        if form.validate() == False:
            return render_template("payment.html", form=form)
        else:
            return render_template("search.html")
        # else:
        #     # Query the database and deposit the amount and subtract from giver

    return render_template("payment.html")

我想让在搜索功能中选择的朋友发送到支付功能。任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: python html flask parameter-passing


    【解决方案1】:

    从您的 HTML 表单开始。表单的action 属性指示表单继续提交的位置。然后你可以只为用户添加一个提交按钮。

    <form method="POST" action="payment">
        <div class="form-group" align="center">
            <select name="friendToPay" class="vertical-menu">
                {% for friend in strFriendList %}
                    <option value="{{ friend }}">{{ friend }}</option>
                {% endfor %}
            </select>
    
            <input type="submit" value="Submit" class="btn-primary" />
        </div>
    </form>
    

    然后你可以像这样处理它:

    @app.route("/payment", methods=['POST'])
    def payment():
        if request.form.get('friendToPay'):
            # Run your logic here
    
        return render_template("payment.html")
    

    为了清楚您所问的问题,我删除了表单验证。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    相关资源
    最近更新 更多