mysql 501不支持的方法('POST ')

2ic8powd  于 2023-06-04  发布在  Mysql
关注(0)|答案(1)|浏览(253)

日安
我试图创建一个简单的登录页面,但我不断得到一个错误:“消息:不支持的方法('POST')。错误代码说明:501 -服务器不支持此操作。“我已经附上了我的代码的图片,请协助

from flask import Flask, render_template, request, redirect, url_for, session
from flask_mysqldb import MySQL, pymysql
import hashlib

app = Flask(__name__)
app.secret_key = "Core123"

# Enter your database connection details below
app.config["MYSQL_HOST"] = "localhost"
app.config["MYSQL_USER"] = "root"
app.config["MYSQL_PASSWORD"] = "Core@123!"
app.config["MYSQL_DB"] = "pythonlogin"

# Initialize MySQL
mysql = MySQL(app)

@app.route("/http://localhost:5000//", methods=["GET", "POST"])
def login():
    if request.method == "POST":
        username = request.form["username"]
        password = request.form["password"]

        # Retrieve the hashed password
        hash = hashlib.sha1((password + app.secret_key).encode()).hexdigest()

        cursor = mysql.connection.cursor()
        cursor.execute(
            "SELECT * FROM accounts WHERE username = %s AND password = %s",
            (username, hash),
        )
        account = cursor.fetchone()

        if account:
            session["loggedin"] = True
            session["id"] = account[0]
            session["username"] = account[1]
            return redirect(url_for("home"))
        else:
            msg = "Incorrect username/password!"
            return render_template("index.html", msg=msg)

    return render_template("index.html", msg="")

@app.route("/home")
def home():
    if "loggedin" in session:
        return "Logged in successfully!"
    else:
        return redirect(url_for("login"))

if __name__ == "__main__":
    app.run()

我试着测试MYSQL工作台的连通性,将所有的return语句移到了“POST”之后。更新了所有库,如FLASK和Flask MySQL dB

n7taea2i

n7taea2i1#

尝试将主机名和方法从路由中删除。

@app.route("/", methods=["GET", "POST"])

而不是

@app.route("/http://localhost:5000//", methods=["GET", "POST"]) # wrong

相关问题