我有一个页面/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>
答案 0 :(得分:1)
在您的algo.html
模板中,您需要将表单提交回同一网址/contact
,因为您正在检查swipeleft
的值:
<form action="{{ url_for('contact') }}" method="post">
<input type = "submit" name = "submit" value = "swipeleft" />
<input type = "submit" name = "submit" value = "swiperight" />
</form>
答案 1 :(得分:0)
我想你的问题就在这里:
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']:
答案 2 :(得分:0)
试一试:
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')