1

我有一个页面/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>
4

3 回答 3

2

在您的algo.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>
于 2013-11-07T05:17:21.717 回答
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']:
于 2013-11-05T18:41:21.780 回答
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')
于 2013-11-07T05:09:21.957 回答