[英]Flask Python Buttons not Responding
我有一个页面/contact.html。 它有一个按钮,提交后将我带到用户登录后进入第二页(algo.html)。在第二页上,我有两个按钮,但是我都无法响应。 这是我的代码:
@app.route('/contact', methods = ['GET', 'POST'])
def contact():
form = ContactForm()
if request.method == 'POST':
return render_template('algo.html')
if request.method == 'POST' and request.form['submit'] == 'swipeleft':
print "yes"
在contact.html上,我有:
<form action="{{ url_for('contact') }}" method=post>
{{ form.hidden_tag() }}
{{ form.name.label }}
{{ form.name }}
{{ form.submit }}
在algo.html上,我有:
<input type = "submit" name = "submit" value = "swipeleft" method=post>
<input type = "submit" name = "submit" value = "swiperight" method=post>
在algo.html
模板中,您需要将表单提交回相同的url /contact
因为这是您要检查swipeleft
值的swipeleft
:
<form action="{{ url_for('contact') }}" method="post">
<input type = "submit" name = "submit" value = "swipeleft" />
<input type = "submit" name = "submit" value = "swiperight" />
</form>
我想你的问题在这里:
if request.method == 'POST':
return render_template('algo.html')
if request.method == 'POST' and request.form['submit'] == 'swipeleft':
print "yes"
对于第一个if语句,它将始终返回True,并且该函数将返回渲染的模板。 它永远不会检查第二个if语句。
只需切换位置,以便它将检查POST请求以及表单是否已提交。
if request.method == 'POST' and request.form['submit'] == 'swipeleft':
print "yes"
if request.method == 'POST':
return render_template('algo.html')
要么
if request.method == 'POST':
if request.form['submit'] == 'swipeleft':
print "yes"
return render_template('algo.html')
编辑:您在这里犯了一个严重的错误:
<input type = "submit" name = "submit" value = "swipeleft" method=post>
更改为:
<form method="post" action="URL" > # change URL to your view url.
<input type="submit" name="swipeleft" value ="swipeleft">
</form>
现在,在您看来,请执行以下操作:
if request.method == 'POST' and request.form['swipeleft']:
试试吧:
if request.method == 'POST':
if request.form.get('submit') == 'swipeleft':
print "first part"
elif request.form.get('submit') == 'swiperight':
print "second part"
return render_template('algo.html')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.