【问题标题】:Flask Python Buttons not Responding烧瓶 Python 按钮没有响应
【发布时间】:2013-11-07 13:17:21
【问题描述】:

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

【问题讨论】:

    标签: python html flask


    【解决方案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>
    

    【讨论】:

      【解决方案2】:

      我猜你的问题在这里:

      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']:
      

      【讨论】:

      • 我尝试了第一个建议,但没有奏效。这与contact.html上的原始“提交”按钮和algo.html上的swipleft按钮有关吗?
      • 你能在有输入按钮的地方张贴algo.html 表单吗?
      • 你的意思是forms.py吗?我没有,我想这就是为什么?我需要投入什么才能让它发挥作用?
      • 不不,我的意思是 html 代码。 &lt;input type = "submit" name = "submit" value = "swipeleft" method=post&gt; 只需发布表格&lt;form action="url"&gt;....&lt;form/&gt;
      • 那么你的视图 URL 的 URL 是指“routes.py”吗?
      【解决方案3】:

      试试看:

      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-16
        • 2018-07-13
        • 1970-01-01
        • 2018-08-17
        • 2017-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多