包含验证其他表单的提交按钮

2024-05-06 12:33:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我的页面上有两个表单,每个表单都有自己的字段和提交按钮,但每当我使用其中任何一个表单时,它们总是检查整个页面字段,而不仅仅是它们所包含的表单

simulator.html有两种形式,第一种是:

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

我认为通过专门调用所提到的字段并将其包含到表单类中,可以避免整体验证,但现在我无法使提交按钮集中在它们自己的表单上。请问我该怎么修


1条回答
网友
1楼 · 发布于 2024-05-06 12:33:45

你有两种形式。一个在'form-1'分区内。一个在'form-2'分区内。每个分区都有自己的<form></form>标记,这意味着它们是单独的窗体。第一种形式包含两个<;输入>;字段:一个名为capital_html,一个名为price_html,还有一个名为btn的按钮。第二种形式有一个名为<input>ticker_symbol_html字段和一个名为btn的按钮

假设用户填写前两个字段并单击第一个按钮。它将向<;表格>;标签在数据中,它将发送三种信息:

capital_html=xxx
price_html=yyy
btn=submit

就这样。这就是你能得到的。您无法从另一个窗体获取任何字段。如果用户单击另一个表单,您将得到

ticker_symbol_html=xxx
btn=submit

正如您所看到的,问题是您没有简单的方法来判断提交了哪个表单。如果两个URL必须使用相同的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.

相关问题 更多 >