[英]Passing data from a GET request to a POST request with Python Flask
[英]Struggling to get data from POST request data from a HTML form with flask
我一直在使用 python 庫 flask 開發網站后端,但是每當我嘗試從 POST 請求中獲取表單數據(因為它需要安全發送)時,我總是會收到 405 - Method not allowed 錯誤。 在提交用戶的 email 和密碼之后,它只是意味着將他們發送到一個非常基本的 html 頁面,上面有他們的名字和 email (代碼更容易解釋)。
python 代碼:
# reroutes user to login page html file
@app.route("/")
@app.route("/login", methods=["GET", "POST"])
def reroute_login():
return flask.render_template("login.html")
# returns the login page html file
@app.route("/login", methods=["GET", "POST"])
def login():
if flask.request.method == "POST":
print("Reached")
email = flask.request.form["email"]
password = flask.request.form["password"]
print(email, password)
return flask.redirect(flask.url_for("user", usr=email))
else:
return flask.render_template("login.html")
@app.route("/<usr>")
def user(usr):
return f"<h1>{usr}</h1>" # here is that very basic html page I mentioned but it won't reroute to this page
登錄.html:
<form action="#" method="post">
<h1>Login</h1>
<input type="text" name="email" placeholder="Email">
<input type="password" name="password" placeholder="Password">
<a href="#">Forgot Your Password</a>
<input type="submit" class="button1" value="submit" />
</form>
請知道我對 Flask 和后端 web 開發人員作為一個整體非常陌生,但任何形式的幫助將不勝感激:)
嘗試將 login.html 中的操作從#
更改為/login
。
並且您已經定義了多個具有相同名稱的路由。 刪除第一個登錄路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.