烧瓶多次提交按钮

时间:2018-08-24 15:50:38

标签: html flask jinja2 form-submit

我正在使用flask和jinja2创建一个简单的Web应用程序,以提供一个简单的sklearn算法进行预测。

在我的html中,我需要获取4个变量:客户端id,textid,textid1,textid2

当我将所有按钮都连接到一个提交按钮时,该按钮当前有效。但是我想有两个提交按钮,使客户端ID在页面顶部提交,而textid东西在页面底部。当我尝试有两个提交按钮时,它将导致页面刷新,并且我无法将客户端ID连接到3个textid变量。

    <div class="col">
        <div class="form-group">
        <label>Enter Customer ID or leave blank for random selection </label>
         <form method="POST">
            <input name="text", id='text', placeholder="Client ID #", value="{{ client_id|round|int }}" >
            <br>
            <label>Enter 3 suggestions</label>
            <br>
            <input name="textid", placeholder="Suggested Model ID #", value="{{ request.form['textid'] }}"/>
            <input name="textid1", placeholder="Suggested Model ID #", value="{{ request.form['textid1'] }}"/>
            <input name="textid2", placeholder="Suggested Model ID #", value="{{ request.form['textid2'] }}"/>

            <input type="submit" >
          </form>
      </div>

我只是像这样在烧瓶中抓起它:

@app.route('/suggestion', methods=['GET', 'POST'])
def with_suggestions():

try:
    client_id=request.form['text']

except:
#custom function when client id is not entered to get random one
    client_id = recommender.random_client_id()
try:
    model_id=request.form['textid']
    model_id1=request.form['textid1']
    model_id2=request.form['textid2']
#other functional code after this

如何拆分HTML以获取两个提交按钮?谢谢!

2 个答案:

答案 0 :(得分:0)

我对您的代码进行了一些更改。

index.html

<div class="col">
    <div class="form-group">
    <label>Enter Customer ID or leave blank for random selection </label>

    <form method="POST" action={{url_for('suggestion')}}>
        <input name="text", id='text', placeholder="Client ID" >
        <input type="submit" >
    </form>
    <form method="POST" action={{url_for('suggestion')}}>
        <input name="textid", id='text', placeholder="Textid1">
        <input name="textid1", id='text', placeholder="textid2  ">
        <input name="textid2", id='text', placeholder="Textid3">
        <input type="submit" value="Submit">
    </form>
  </div>

main.py

from flask import Flask
from flask import render_template, url_for, request, redirect
app = Flask(__name__)

@app.route('/suggestion', methods=['GET', 'POST'])
def suggestion():
    if request.method == 'POST':
        try:
            client_id=request.form['text']
        except:
            model_id=request.form['textid']
            model_id1=request.form['textid1']
            model_id2=request.form['textid2']
    return render_template('index.html')        

if __name__ == '__main__':
    app.run()

注意:值存储在变量中,请打印以查看

答案 1 :(得分:0)

现在,您已经更新了代码,您所要做的就是隐藏隐藏的输入,以识别点击的来源。像我在下面

一样,从您的url_for中删除前导斜杠
<div class="col">
<div class="form-group">
<label>Enter Customer ID or leave blank for random selection </label>

<form method="POST" action={{url_for('suggestion')}}>
    <input name="text", id='text', placeholder="Client ID" >
    <input type="hidden" name="btn_identifier" value="client_id_identifier" />
    <input type="submit" >
</form>
<form method="POST" action={{url_for('suggestion')}}>
    <input name="textid", id='text', placeholder="Textid1">
    <input name="textid1", id='text', placeholder="textid2  ">
    <input name="textid2", id='text', placeholder="Textid3">
    <input type="hidden" name="btn_identifier" value="text_id_identifier" />
    <input type="submit" value="Submit">
</form>

main.py

from flask import Flask
from flask import render_template, url_for, request, redirect
app = Flask(__name__)

@app.route('/suggestion', methods=['GET', 'POST'])
def with_suggestions():
    if request.methods == 'POST':
        if request.form['btn_identifier'] == 'client_id_btn':
            try:
                client_id=request.form['text']
            except:
                # I think this would go in the second elif statement
                model_id=request.form['textid']
                model_id1=request.form['textid1']
                model_id2=request.form['textid2']
        elif request.form['btn_identifer'] == 'text_id_btn':
            # run some code to handle a click that was originated from the second button
    return render_template('index.html')        

if __name__ == '__main__':
    app.run()