73

我正在尝试在页面上创建两个按钮。每一个我都想在服务器上执行不同的 Python 脚本。到目前为止,我只设法使用一个按钮来获取/收集。

def contact():
  form = ContactForm()

  if request.method == 'POST':
    return 'Form posted.'

  elif request.method == 'GET':
     return render_template('contact.html', form=form)

根据按下的按钮,我需要更改什么?

4

7 回答 7

145

为您的两个按钮提供相同的名称和不同的值:

<input type="submit" name="submit_button" value="Do Something">
<input type="submit" name="submit_button" value="Do Something Else">

然后在您的 Flask 视图函数中,您可以知道哪个按钮用于提交表单:

def contact():
    if request.method == 'POST':
        if request.form['submit_button'] == 'Do Something':
            pass # do something
        elif request.form['submit_button'] == 'Do Something Else':
            pass # do something else
        else:
            pass # unknown
    elif request.method == 'GET':
        return render_template('contact.html', form=form)
于 2013-11-05T17:16:59.780 回答
38

这样做的适当方法:

@app.route('/')
def index():
    if form.validate_on_submit():
        if 'download' in request.form:
            pass # do something
        elif 'watch' in request.form:
            pass # do something else

watchdownload按钮放入您的模板中:

<input type="submit" name="download" value="Download">
<input type="submit" name="watch" value="Watch">
于 2015-01-14T21:42:58.933 回答
19

以防有人像我一样还在寻找并看到这篇 SO 帖子。

<input type="submit" name="open" value="Open">
<input type="submit" name="close" value="Close">

def contact():
    if "open" in request.form:
        pass
    elif "close" in request.form:
        pass
    return render_template('contact.html')

简单,简洁,并且有效。甚至不需要实例化表单对象。

于 2018-03-16T18:14:40.263 回答
17

我通过以下方式处理它:

<html>
    <body>

        <form method="post" action="/">

                <input type="submit" value="Encrypt" name="Encrypt"/>
                <input type="submit" value="Decrypt" name="Decrypt" />

        </form>
    </body>
</html>
    

蟒蛇代码:

    from flask import Flask, render_template, request
    
    
    app = Flask(__name__)
    
    
    @app.route("/", methods=['GET', 'POST'])
    def index():
        print(request.method)
        if request.method == 'POST':
            if request.form.get('Encrypt') == 'Encrypt':
                # pass
                print("Encrypted")
            elif  request.form.get('Decrypt') == 'Decrypt':
                # pass # do something else
                print("Decrypted")
            else:
                # pass # unknown
                return render_template("index.html")
        elif request.method == 'GET':
            # return render_template("index.html")
            print("No Post Back Call")
        return render_template("index.html")
    
    
    if __name__ == '__main__':
        app.run()
于 2018-03-12T17:21:48.977 回答
5

我认为这个解决方案很好:

@app.route('/contact', methods=['GET', 'POST'])
def contact():
    form = ContactForm()
    if form.validate_on_submit():
        if form.submit.data:
            pass
        elif form.submit2.data:
            pass
    return render_template('contact.html', form=form)

形式:

class ContactForm(FlaskForm):

    submit = SubmitField('Do this')
    submit2 = SubmitField('Do that')
于 2018-07-17T15:27:03.840 回答
1

将(不同的)名称属性应用于两个按钮,例如

<button name="one">

并在 request.data 中捕获它们。

于 2013-11-05T17:16:29.520 回答
0

这项工作对我来说 .py

if request.method == "POST":
     if request.form.get('new Token'):
#something
     if request.form.get('custom Token'):
#something diferent

.html

<form method="post" >
<input type="submit" name="new Token" value="new Token">
<input type="submit" name="custom Token" value="custom Token">
</form>

有效,因为您不压迫的按钮将 None 返回给 python。美好的一天

于 2021-05-07T02:01:03.213 回答