2017-04-30 340 views
-1

我是燒瓶,HTML,Jinja的新手,我無法將值從一個模板傳遞到另一個模板。我看了很多關於SO的其他頁面,但沒有人遇到過相同的問題。我有一個帶有下拉選擇菜單的頁面和一個提交按鈕,用戶在做出選擇後可以重定向。我的問題是,我不知道如何將該選項傳遞給下一頁上的表單。下面是我的HTML表單代碼:燒瓶從一個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") 

我想在發送到支付功能的搜索功能選擇的朋友。任何幫助將不勝感激,謝謝!

回答

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") 

我已經刪除了清晰的關於你問的問題着想表單驗證。

相關問題