簡體   English   中英

包含驗證其他 forms 的提交按鈕

[英]Contained submit button validating other forms

我的頁面上有兩個 forms,每個都有自己的字段和提交按鈕,但是每當我使用它們中的任何一個時,它們總是檢查整個頁面字段,而不僅僅是它們包含的表單...

模擬器.html有兩個forms,第一個是這樣的:

<div class="forms">
 <div class="form-1">
  <form method="post" action="{{ url_for('core.simulator') }}">
   <div class="container">
    <div class="row g-3">
     <div class="col-sm-3">
      <label class="form-label"><b>Capital:</b></label>
      <input type="text" class="form-control" name="capital_html" required>
     </div>
     <div class="col-sm-3">
      <label class="form-label"><b>Price:</b></label>
      <input type="text" class="form-control" name="price_html" required>
     </div>
    </div>
   </div>
   <br>
   <div class="d-grid gap-2 d-md-flex justify-content-md-start">
    <button id="btn" type="submit" class="btn btn-info">Launch simulation!</button>
   </div>
  </form>
  <br>
   <p>Units to purchase: <b>{{simulation_units}}</b>
 </div>
</div>

第二個是這樣的:

<h3> Screener</h3>
 <div class="forms">
  <div class="form-2">
   <form method="post" action="{{ url_for('core.simulator') }}">
    <div class="container">
     <div class="row g-3">
      <div class="col-sm-3">
       <label class="form-label"><b>Ticker:</b></label>
       <input type="text" class="form-control" name="ticker_symbol_html" placeholder="Enter Ticker Symbol" required>
      </div>
     </div>
    </div>
    <br>
   <div class="d-grid gap-2 d-md-flex justify-content-md-start">
     <button id="btn" type="submit" class="btn btn-info">Launch!</button>
   </div>
   <p>Symbol <b>{{simulation_symbol}}.
  </form>
 </div>
</div>

views.py 文件有每個的后端代碼,第一個是這樣的:

def capital_simulator():
    if request.method == 'POST':
       simulation_capital = request.form.get('capital_html', '')
       simulation_price = request.form.get('price_html', '')

       try:
          simulation_units = math.floor(float(simulation_capital) / float(simulation_price))
       except KeyError:
       simulation_units == 0

       return render_template('simulator.html',form1=form,capital_html=simulation_capital,price_html=simulation_price,simulation_units=simulation_units)

第二種形式的后端腳本是這樣的:

def screener():
    if request.method == 'POST':
       ticker_symbol = request.form.get('ticker_symbol_html', '')

       return render_template('simulator.html',ticker_symbol=ticker_symbol_html,ticker_symbol=simulation_symbol)

我想通過專門調用提到的字段並將它們包含到 forms 類中,我會避免整體驗證,但現在我沒有讓提交按鈕專注於他們自己的 forms。 請問我該如何解決這個問題?

您有兩個 forms。 一個在'form-1' div 內。 一個在'form-2' div 內。 每個都有自己的<form></form>標簽,這意味着它們是獨立的 forms。 第一個表單包含兩個 <input> 字段:一個名為capital_html ,一個名為price_html ,一個名為btn的按鈕。 第二種形式有一個名為ticker_symbol_html<input>字段和一個名為btn的按鈕。

假設用戶填寫了前兩個字段並單擊了第一個按鈕。 這將向 <form> 標記中的 URL 發送請求。 在數據中,它將發送三件事:

capital_html=xxx
price_html=yyy
btn=submit

而已。 這就是你所得到的。 您不會從其他表單中獲得任何字段。 如果用戶單擊另一個表單,您將得到的只是

ticker_symbol_html=xxx
btn=submit

如您所見,問題在於您沒有簡單的方法來判斷提交了哪個表單。 如果您必須為兩者使用相同的 URL,解決此問題的常用方法是添加一個與數據一起發送的隱藏字段,例如:

<input type=hidden name="ident" text="form1">

在第二個中:

<input type=hidden name="ident" text="form2">

現在,您的處理程序可以說

    if request.form.get("ident") == "form1":
        # Go handle first form.
    else:
        # Go handle second form.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM