通过Python Flask从一个HTML输入中获取多个值 [英] Get multiple values from one HTML input through Python Flask

查看:1422
本文介绍了通过Python Flask从一个HTML输入中获取多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态生成的包含文本框的行的默认值(order_quantity)。基本上在一篇文章,我希望我的项目表在SQL中根据他们的ID更新这些文本框的值。

  {%for i in items%} 
< tr>
< td>
...
< / td>
< td>
...
< / td>
< td>
< div class =form-group>
< input type =numberid =amountname =amountvalue ={{i.order_quantity}}>
< / div>
< / td>
< td>
...
< / td>
< / tr>
{%endfor%}

传入order_quantity或item ID没有问题不是很明显。



HTML:

 < input type = numberid =amountname =amountvalue ={{i.item_id}} {{i.order_quantity}}> 

Python:

  amount = request.form.getlist('amount')
print amount
for i in amount:
print i

总而言之,如何简单地将数量和相关ID传递给每个文本框,以方便服务器端提取?

解决方案

我想你可以通过添加一个隐藏的输入字段来实现你想要的item_id



例子:

 < td> 
< div class =form-group>
< input type =numberid =amountname =amountvalue ={{i.order_quantity}}>
< input type =hiddenname =item_idvalue ={{i.item_id}}/>
< / div>
< / td>

当您在Flask中接收到输入时,您可以将ID与自订单保留以来的金额。
$ b $ pre $ amount = request.form.getlist('amount')
item_ids = request.form.getlist(' item_id')
for item_id,idx in enumerate(item_id):
amount = amount [idx]
#使用item_id和金额
/ pre>

I have a dynamically generated number of rows containing text boxes in a table with a default value (order_quantity). Basically on a post I want my items table in sql to be updated with the values of these text boxes according to their ID's.

{% for i in items %}
        <tr>
            <td>
                ...
            </td>
            <td>
                ...
            </td>
            <td>
                <div class="form-group">
                    <input type="number" id="amount" name="amount"  value="{{ i.order_quantity }}">
                </div>
            </td>
            <td>
             ...
             </td>
        </tr>
        {% endfor %}

There's no problem passing in the order_quantity OR the item ID but not both obviously.

HTML:

<input type="number" id="amount" name="amount"  value="{{i.item_id}} {{ i.order_quantity }}">

Python:

amount = request.form.getlist('amount')
    print amount
    for i in amount:
        print i

In summary, how can I pass both the quantity and its relative id out in a simplistic manner for easy server-side extraction for each text box?

解决方案

I think you can achieve what you want by adding a hidden input field that holds the item_id

Example:

<td>
    <div class="form-group">
        <input type="number" id="amount" name="amount"  value="{{ i.order_quantity }}">
        <input type="hidden" name="item_id" value="{{ i.item_id }}"/>
    </div>
</td>

When you receive the input in Flask, you can match the id's to the amounts since the order is preserved.

amounts = request.form.getlist('amount')
item_ids = request.form.getlist('item_id')
for item_id, idx in enumerate(item_id):
    amount = amounts[idx]
    # do something with item_id and amount

这篇关于通过Python Flask从一个HTML输入中获取多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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