烧瓶Python按钮

时间:2013-11-05 17:07:28

标签: python html button flask

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

获取/收集一个按钮
def contact():
  form = ContactForm()

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

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

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

7 个答案:

答案 0 :(得分:88)

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

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

答案 1 :(得分:20)

这样做的恰当方式:

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

答案 2 :(得分:6)

如果有人还在寻找并且像我一样偶然发现了这个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" request.form:
        pass
    return render_template('contact.html')

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

答案 3 :(得分:3)

我按以下方式处理:

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

答案 4 :(得分:2)

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

<button name="one">

并在request.data中捕获它们。

答案 5 :(得分:1)

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

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

答案 6 :(得分: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。 美好的一天