Python 按钮没有响应
我有一个页面/contact.html。它有一个按钮,当提交时会将我带到第二个页面(algo.html),用户已登录。在第二页上,我有两个按钮,但两个按钮都无法响应。这是我的代码:Python 按钮没有响应,python,html,flask,Python,Html,Flask,我有一个页面/contact.html。它有一个按钮,当提交时会将我带到第二个页面(algo.html),用户已登录。在第二页上,我有两个按钮,但两个按钮都无法响应。这是我的代码: @app.route('/contact', methods = ['GET', 'POST']) def contact(): form = ContactForm() if request.method == 'POST': return render_template('alg
@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 }}
<input type = "submit" name = "submit" value = "swipeleft" method=post>
<input type = "submit" name = "submit" value = "swiperight" method=post>
{{form.hidden_tag()}}
{{form.name.label}
{{form.name}
{{form.submit}
在algo.html上,我有:
<form action="{{ url_for('contact') }}" method=post>
{{ form.hidden_tag() }}
{{ form.name.label }}
{{ form.name }}
{{ form.submit }}
<input type = "submit" name = "submit" value = "swipeleft" method=post>
<input type = "submit" name = "submit" value = "swiperight" method=post>
我想你的问题在这里:
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')
或
编辑:
你犯了一个严重的错误:
<input type = "submit" name = "submit" value = "swipeleft" method=post>
我想你的问题在这里:
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')
或
编辑:
你犯了一个严重的错误:
<input type = "submit" name = "submit" value = "swipeleft" method=post>
试试看:
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')
试试看:
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')
在
algo.html
模板中,您需要将表单提交回同一url/contact
,因为您要在那里检查swipleft
的值:
<form action="{{ url_for('contact') }}" method="post">
<input type = "submit" name = "submit" value = "swipeleft" />
<input type = "submit" name = "submit" value = "swiperight" />
</form>
在您的
算法.html
模板中,您需要将表单提交回同一url/contact
,因为您要在那里检查swipeleft
的值:
<form action="{{ url_for('contact') }}" method="post">
<input type = "submit" name = "submit" value = "swipeleft" />
<input type = "submit" name = "submit" value = "swiperight" />
</form>
我尝试了最初的建议,但没有成功。这是否与contact.html上原来的“提交”按钮和algo.html上的swipleft按钮有关?你能把algo.html
form张贴到你有输入按钮的地方吗?你是说forms.py吗?我没有,我想这就是为什么?我需要在里面放些什么才能让它工作呢?不,不,我指的是html代码
只需发布表单…
,那么您的视图URL的URL是指“routes.py”吗?我尝试了最初的建议,但没有成功。这是否与contact.html上原来的“提交”按钮和algo.html上的swipleft按钮有关?你能把algo.html
form张贴到你有输入按钮的地方吗?你是说forms.py吗?我没有,我想这就是为什么?我需要在里面放些什么才能让它工作呢?不,不,我指的是html代码<代码>只需发布表单…
,那么您的视图URL的URL是指“routes.py”吗?