【问题标题】:flask multiple submit button烧瓶多个提交按钮
【发布时间】:2018-08-24 15:50:37
【问题描述】:

我正在使用 flask 和 jinja2 创建一个简单的网络应用程序来提供一个简单的 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 以获得两个提交按钮?谢谢!!

【问题讨论】:

标签: html flask jinja2 form-submit


【解决方案1】:

现在您已经更新了代码,您需要做的就是添加隐藏输入来识别点击的来源。也像我在下面所做的那样从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()

【讨论】:

  • 我的问题是:如何仅在特定表单上使用 validate_on_submit ......而不是在字段集中评估所有内容
【解决方案2】:

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

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

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

【讨论】:

  • 看看我根据你更新的代码更新的答案
  • 对不起,我很困惑,以为您是提出这个问题的人,却没有意识到这是一个不同的答案。
【解决方案3】:

我已经简化了从多个按钮获取信息的过程。请注意,“请求”方法需要 python 烧瓶框架。

home.html

<div class="container mt-5">
    <div class="row col-4">
        <form method="POST" class="form-register">

            <input type="submit" name="submit_button" value="Add Email">
            <input type="submit" name="submit_button" value="Clear Recipients">
        </form>
    </div>
</div>

运行.py

if request.method == 'POST':
        if request.form['submit_button'] == 'Add Email':
            print("add email")
        
        elif request.form['submit_button'] == 'Clear Recipients':
            print("clear recipients")

您可以参考提供的链接以获取更多示例

https://www.codegrepper.com/code-examples/python/checking+if+button+pressed+flask

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多