如何使用request.form.get从表单获取多个元素 [英] How to get multiple elements from a form using request.form.get

查看:2208
本文介绍了如何使用request.form.get从表单获取多个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在html文件中有一个表单,该表单正在创建新的输入字段,用户应在其中输入一些信息.后来,我想将所有这些值传递到我的python主文件(application.py)中并对其进行处理.问题是我不知道如何将多个值传递给python.通常我会使用

I have a form inside an html file, this form is creating new input fields in which the user is supposed to type some info. Later, i wanna pass all those values into my python main file (application.py)and do somethings with them. The problem is that i don't know how to pass several values into python. Normally i would use this

request.form.get(用户名")

request.form.get("username")

向我返回html文件中名为用户名"的输入字段的值.现在,我有几个输入字段,这些输入字段是在用户按下按钮时生成的:

which returns me the value of the input field named "username" inside my html file. Now, i have several input fields which are generated when the user pushes a button:

$("#add_dream").click(function(){
    $("#"+x).append('<button type="submit" style="height:30px;" class="delete" 
    id="ix">Remove</button>')
    document.getElementById("ix").setAttribute("id","delbuttdream"+j)
}

这不是全部代码,但可能有助于理解我的意思.可以根据用户的需要多次删除创建此新字段,因此,字段名称或其ID不会遵循直接的顺序(1,2,3,4 ...).我想知道是否无论如何我都可以使用request.form.python从python调用.获取同一个类别或具有特定ID的所有元素,而不仅仅是其中一个按名称命名

This is not the whole code, but it may help to understand what i'm saying. This new fields can be created an deleted as many times as the user wants to, so the name of the fields or their ids don't follow a straight order (1,2,3,4...). I wanna know if there's anyway in which i can call from python using request.form.get all the elements of the same clase or with certain id and not only one of them by name

推荐答案

示例形式:

Items in random order</br>
<form method="POST">
<input name="item4" value="val4"/></br>
<input name="item2" value="val2"/></br>
<input name="item1" value="val1"/></br>
<input name="item3" value="val3"/></br>
<button type="submit">OK</button>
</form>

request.form的行为类似于字典,您可以使用request.form.items()来获取所有键和值并对其进行过滤.

request.form behaves like dictionary and you can use request.form.items() to get all keys and values and filter them.

for key, val in request.form.items():
    #print(key,val)
    if key.startswith("item"):
        print(key, val)

request.form.keys()仅获取要过滤和排序的键.

or request.form.keys() to get only keys to filter and sort them.

keys = request.form.keys()
keys = [key for key in keys if key.startswith("item")]
keys = sorted(keys)

for key in keys:
    #print(key, request.form[key])
    print(key, request.form.get(key))


最低工作代码:


Minimal working code:

from flask import Flask, request, render_template_string

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':

        print('--- original order ---')

        for key, val in request.form.items():
            if key.startswith("item"):
                print(key, val)

        print('--- sorted ---')

        keys = request.form.keys()
        keys = [key for key in keys if key.startswith("item")]
        keys = sorted(keys)

        for key in keys:
            #print(key, request.form[key])
            print(key, request.form.get(key))

    return render_template_string('''Items in random order</br>
<form method="POST">
<input name="item4" value="val4"/></br>
<input name="item2" value="val2"/></br>
<input name="item1" value="val1"/></br>
<input name="item3" value="val3"/></br>
<button type="submit">OK</button>
</form>
''')

if __name__ == '__main__':
    app.run(debug=True)

这篇关于如何使用request.form.get从表单获取多个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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