Python Flask-从具有多个文本框的表单接收数据 [英] Python Flask - Receive data from form with multiple textboxes

查看:51
本文介绍了Python Flask-从具有多个文本框的表单接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生成一个通过SQL查询返回几行的网页.我将它们显示在html表中,其中1列是文本框.我遍历每行新行的sql结果,所有文本框字段的名称都相同.

I am generating a web page form a sql query that returns several rows. Im displaying them in a html table with 1 column being a text box. I loop trough the sql results for each new line, the textbox fields all end up having the same name.

单击提交按钮时,我仅从第一个文本框中接收一个值.

When clicking the submit button i am only recieving the one value from the first textbox.

这里是HTML即时通讯使用

Here is the HTML im using

<form action="/commit_changes" method=post>
    <ul class=naming>
  <h2>{{ Overview }}</h2>
    {% for entry in sensor_names %}
        <li>{{ entry.sensor_add|safe }}
        <input type="text" name="sensor_name" value={{ entry.sensor_name|safe }}>
        <input type="checkbox" name="del" value="del">
        <li> <br> </li>
    {% endfor %}
</ul>

<input type="submit" name="submit" value="Commit Changes">
</form>

这是flask中的python代码:

Here is the python code in flask:

@app.route('/commit_changes', methods=['GET', 'POST'])
def commit_changes():
    print(request.form['sensor_name'])
    return redirect('settings')

我猜测将数据发送回python的唯一方法是某种JavaScript,以遍历文本框并一次发送一个.这就是我卡住的地方:),希望有人能指出我正确的方向.光盘

Im having a guess that the only way to send the data back to python is some sort of javascript to loop through the textboxes and send them one at a time. This is where im stuck :), hope someone can point me in the right direction. CD

推荐答案

您必须为 input 文本字段分配唯一的 name 属性.

You have to assign the unique name attribute for the input text field.

<form action="/commit_changes" method=post>
        <ul class=naming>
      <h2>{{ Overview }}</h2>
        {% for entry in sensor_names %}
            <li>{{ entry.sensor_add|safe }}
            <input type="text" name="sensor_name{{entry.id}}" value={{ entry.sensor_name|safe }}>
            <input type="checkbox" name="del" value="del">
            <li> <br> </li>
        {% endfor %}
    </ul>

    <input type="submit" name="submit" value="Commit Changes">
    </form>

如果您没有 sensor_names 对象列表的 id 属性,则可以尝试使用 sensor_name {{forloop.counter}} 而不是 sensor_name {{entry.id}}

if you don't have id attribute for sensor_names object list.you could try sensor_name{{forloop.counter}} instead of sensor_name{{entry.id}}

这篇关于Python Flask-从具有多个文本框的表单接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆