且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过Python Flask从一个HTML输入中获取多个值

更新时间:2023-11-22 12:01:40

我想你可以通过添加一个隐藏的输入字段来实现你想要的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