烧瓶 Python 按钮

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

def contact():
form = ContactForm()


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


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

我需要改变什么基于按钮按下?

242952 次浏览

Apply (different) name attribute to both buttons like

<button name="one">

and catch them in request.data.

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:

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)

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 watch and download buttons into your template:

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

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


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

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

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

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.

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

this work to me .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>

works because the button that you do not oppress ,return None to python. nice day