html-Flask Python按钮

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

def contact():
  form = ContactForm()

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

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

我需要根据按下的按钮进行哪些更改?

dojogeorge asked 2020-02-21T20:00:04Z
6个解决方案
93 votes

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

<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)
Miguel answered 2020-02-21T20:00:21Z
24 votes

执行此操作的适当方法:

@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

将275779743748928928和download按钮放入模板:

<input type="submit" name="download" value="Download">
<input type="submit" name="watch" value="Watch">
Alexander.Iljushkin answered 2020-02-21T20:00:46Z
10 votes

万一有人还在找,像我一样碰到这样的帖子。

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

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

poply answered 2020-02-21T20:01:10Z
4 votes

我通过以下方式处理它:

<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代码:

    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()
rashid answered 2020-02-21T20:01:34Z
2 votes

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

<button name="one">

并将其捕获到request.data中。

Paweł Pogorzelski answered 2020-02-21T20:01:58Z
1 votes

我认为此解决方案很好:

@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')
slinden answered 2020-02-21T20:02:22Z
translate from https://stackoverflow.com:/questions/19794695/flask-python-buttons