无法将html表单中的值提取到我的python代码中

无法将html表单中的值提取到我的python代码中,python,Python,我无法将html表单中的信息提取到python代码中。我多次检查代码,但似乎没有问题。请检查我的密码,告诉我出了什么问题。多谢各位 @app.route("/search",methods=["POST","GET"]) def search1(): #python code var=request.form.get("search2") sear=str(var) print(var,sear) r

我无法将html表单中的信息提取到python代码中。我多次检查代码,但似乎没有问题。请检查我的密码,告诉我出了什么问题。多谢各位

@app.route("/search",methods=["POST","GET"]) 
def search1():                                #python code
    var=request.form.get("search2")
    sear=str(var)
    print(var,sear)
    return " "
<html>                      <!--html code-->
<head>
    <title>hi there</title>
</head>
<body>
    <h1 style="font-family:verdana; font-style:italic;">Welcome to the book sea !!!....</h1>
    <form action="{{ url_for('search1') }}" method="get" align:"center">
        <input type="text" name="search2" placeholder="enter details">
        <button>search</button>
    </form>

</body>
@app.route(“/search”,methods=[“POST”,“GET”])
def search1():#python代码
var=request.form.get(“search2”)
sear=str(var)
打印(var,sear)
返回“”
你好
欢迎来到书海!!!。。。。
搜索
将HTML中的“get”改为“post”。因为Flask路由的设置方式不允许使用get请求传递变量

<form action="{{ url_for('search1') }}" method="get" align:"center">
==search.html。。应放置在项目的templates文件夹中===

<html>                      <!--html code-->
<head>
    <title>hi there</title>
</head>
<body>
<h1 style="font-family:verdana; font-style:italic;">Welcome to the book sea !!!....</h1>
<form action="{{ url_for('search1') }}" method="post">
    <input type="text" name="search2" placeholder="enter details">
    <button>search</button>
</form>

</body>

</html>

你好
欢迎来到书海!!!。。。。
搜索

通过重新安装flask应用程序,问题得以解决。谢谢你的努力。

你返回,返回
也许你是想使用
“…”
。上述情况是否正常?您可以共享日志吗?
app.route(..)
是一个装饰程序,所以请使用
@app.route(..)
。您可以参考此代码,在这个github中检查server.pyrepository@DevanshSoni很抱歉,复制错误。@上面的代码运行得很好,但它没有打印我输入的值,而是打印“none none”。它不起作用,即使将方法更改为post,它也不会获取任何内容。@Storm斗篷应该在控制台中打印出sear=str(var)print(var,sear)。它在我的console@stormcloak我添加了HTML。在我这一方效果很好。如果问题仍然不起作用,请使用当前代码更新问题。不要忘记创建templates文件夹,并将search.html和上面的html代码放在其中。使用var=request.form[“search2”]后,它显示“400错误请求”错误。
from flask import Flask, request, render_template

app = Flask(__name__)

@app.route("/")
def index():
    return render_template("search.html")

@app.route("/search",methods=["POST","GET"])
def search1():                                #python code
    if request.method == 'POST':
        var=request.form["search2"]
        sear=str(var)
        print(var,sear)
    return " "

if __name__ == "__main__":
    app.run(debug=True)
<html>                      <!--html code-->
<head>
    <title>hi there</title>
</head>
<body>
<h1 style="font-family:verdana; font-style:italic;">Welcome to the book sea !!!....</h1>
<form action="{{ url_for('search1') }}" method="post">
    <input type="text" name="search2" placeholder="enter details">
    <button>search</button>
</form>

</body>

</html>