python flask |如何在python变量中存储html输入

python flask |如何在python变量中存储html输入,python,html,flask,Python,Html,Flask,在python中使用flask获取html输入并将其存储为python变量 /example.html <form> <input type=text name=string> # a string value <input type=submit> <input type=text name=float> # a float value <input type=submit> </form> /main.py @

在python中使用flask获取html输入并将其存储为python变量

/example.html
<form>
<input type=text name=string>  # a string value
<input type=submit>
<input type=text name=float>  # a float value
<input type=submit>  
</form>

/main.py
@app.route("/")
def get_input():
    example_string = request.args.get('search')
    example_limit = request.args.get('float')
    return render_template('example.html', example_string, example_limit)

/examplefile.py
from main import example_string, example_limit
print(example_string)
print(example_limit)
/example.html
#字符串值
#浮点数
/main.py
@附件路线(“/”)
def get_input():
示例_string=request.args.get('search'))
示例_limit=request.args.get('float'))
返回呈现模板('example.html',example\u字符串,example\u限制)
/examplefile.py
从主导入示例\u字符串,示例\u限制
打印(示例\u字符串)
打印(示例_限制)

这种特殊的方法似乎不起作用,可能是因为request.args.get()没有被正确使用或其他原因。你也许可以看到我在这里做了些什么。请帮助。

您的问题表明您可能需要通过本教程或类似教程:

对于您的问题,有许多方法,例如路线:

@app.route('/test', methods=['POST', 'GET'])
def test():
    if request.method == 'POST':
        #version 1:
        opt1 = request.form.to_dict()
        for key in opt1:
            if key == "string":
                string = opt1[key]
            if key == "float": 
                floata = opt1[key]
        print(string, floata)
        #version 2:
        string2 = request.form["string"]
        float2 = request.form["float"]
        # if you need float : floatvar = float(float2)
        print(float2)
        print(string2)

        return "success"
    else:
        return render_template('example.html')
模板:

<form method="POST">
    <input type="text" name="string"/>
    <input type="text" name="float"/>
    <button type="submit">Submit</button>
</form>

提交

这毫无意义,您想存储输入的值还是它的名称?我想在他们单击“提交”时获取值,这样我就可以在程序的其他地方使用该值。所以最重要的是我要存储输入值