烧瓶 Python 按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19794695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:39:18  来源:igfitidea点击:

Flask Python Buttons

pythonhtmlbuttonflask

提问by dojogeorge

Im trying to create two buttons on a page. Each one I would like to carry out a different python script on the server. So far I have only managed to get/collect one button using

我试图在页面上创建两个按钮。每一个我都想在服务器上执行一个不同的 python 脚本。到目前为止,我只设法使用

def contact():
  form = ContactForm()

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

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

What would I need to change based on button pressed?

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

采纳答案by Miguel

Give your two buttons the same name and different values:

为您的两个按钮指定相同的名称和不同的值:

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

Then in your Flask view function you can tell which button was used to submit the form:

然后在你的 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)

回答by Pawe? Pogorzelski

Apply (different) name attribute to both buttons like

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

<button name="one">

and catch them in request.data.

并在 request.data 中捕获它们。

回答by Alexander.Iljushkin

The appropriate way for doing this:

执行此操作的适当方法:

@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

Put watchand downloadbuttons into your template:

watchdownload按钮放入您的模板中:

<input type="submit" name="download" value="Download">
<input type="submit" name="watch" value="Watch">

回答by rashid

I handle it in the following way:

我按以下方式处理:

<html>
    <body>

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

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

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

Python Code :

蟒蛇代码:

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

回答by poply

In case anyone was still looking and came across this SO post like I did.

万一有人仍在寻找并像我一样看到这篇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')

Simple, concise, and it works. Don't even need to instantiate a form object.

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

回答by slinden

I think this solution is good:

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

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

Form:

形式:

class ContactForm(FlaskForm):

    submit = SubmitField('Do this')
    submit2 = SubmitField('Do that')