【问题标题】:I am trying to pass input from an input field into my flask app我正在尝试将输入字段中的输入传递到我的烧瓶应用程序中
【发布时间】:2019-08-08 21:39:43
【问题描述】:

我正在循环浏览 Jinja 模板中的数据,并且我已将每条记录的 id 组件设置为输入字段的输入。每个输入字段都有一个相应的提交按钮。我正在尝试通过单击提交按钮将输入字段中的数据发送到我的 Flask 应用程序,但没有任何效果。

我尝试将方法设置为 GET,然后在我的 Flask 应用程序中使用 delete = request.args.get('delInput') 但这不起作用。我使用了各种形式的 delinput。

<tbody>
    <ul>
        {%for id in records%}
            <tr>
          <th scope="row">{{loop.index}}</th>
          <td>{{id[0]}}</td>
          <td>{{id[1]}}</td>
          <th> 
            <form method = 'POST'>
            {{setDel.hidden_tag()}} 
            {{setDel.delInput(value = id[2])}}
            {{setDel.delButton(class="btn btn-danger")}} 
            </form>
    </th>
        </tr>
        {%endfor%}
            </tr>
      </tbody> 


elif setDel.validate_on_submit():
            delete = setDel.delInput.data
            with open('/home/matt/Desktop/test.txt','w') as x:
                x.write(str(delete),'testing')

【问题讨论】:

    标签: python forms post flask flask-wtforms


    【解决方案1】:

    语法不正确。首先,要让表单传递数据,每个字段都需要设置“名称”属性。 其次, id[0].. 无效。您的字段要么有一个像{{id['key1']}} 这样的键,否则你使用{{id.0}}, {{id.1}} 等。 for 循环中的 id 名称可能会欺骗您。

    【讨论】:

    • 如果我将表单更改为
      那么我将如何获取数据?另外,我认为 id[0] 字段是有效的,因为数据被放入表中的适当位置,并且我知道我希望烧瓶应用程序接收的数据被放入隐藏的输入字段,因为当我改变HiddenField 到 StringField 我可以看到正确的数据
    • 你没有明白我的意思。输入应具有属性 name='id_name' 以便 request.form['id_name'] 有效。
    猜你喜欢
    • 2020-05-09
    • 2020-10-24
    • 2021-04-22
    • 2021-09-22
    • 2020-07-17
    • 2011-07-31
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多