Flask-它一直显示“方法不允许”

问题描述 投票:0回答:1

我是 Web 开发新手,我正在使用 Flask 来构建我的 Web。当我运行 app.py 并导航到登录页面时,它一直显示“不允许使用方法”。我不确定我在哪里做错了。请帮忙。谢谢你。

这是我的应用程序.py

from flask import Flask,render_template,url_for,redirect,session,request
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash


app = Flask(__name__)

# Configure SQL Alchemy
app.config['SECRET_KEY'] = 'your_secret_key'
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///users.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)

# Database model
class Users(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    username = db.Column(db.String(25),unique=True,nullable=False)
    password = db.Column(db.String(125),nullable=False)
    
    def set_password(self,password):
        self.password_hash = generate_password_hash(password)
        
    def check_password(self,password):
        return check_password_hash(self.password_hash,password)



# Routes

# Home

@app.route("/")
def index():
    if "username" in session:
        return redirect (url_for('home'))
    return render_template ('home.html')

# Login
@app.route("/login",methods=["GET","POST"])
def login():
 if request.method == 'POST':
    username = request.form["Username"]
    password = request.form["Password"]
    check_user = Users.query.filter_by(username=username).first()
    if check_user and check_user.check_password(password):
        session["username"] = username
        return redirect(url_for("dashboard"))
    else:
        return render_template ('login.html')

@app.route('/signup')
def signup():
    return render_template('signup.html')



if __name__ == '__main__':
    with app.app_context():
        db.create_all()
    app.run(debug=True)

这是我的login.html

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <h2>Login</h2>
    <form action="/login" method="post" >
        <input type="text" name="Username" placeholder="Username" required>
        <input type="password" name="Password" placeholder="Password" required>
        <button type="submit">Login</button>
    </form>
    <p>Don't have an account? <a href="{{ url_for('signup') }}">Sign up</a></p>
</body>
</html>

我尝试了很多方法,遵循youtube上的一些结构。没有任何变化。同样的错误。

python flask flask-sqlalchemy flask-login
1个回答
0
投票

由于您没有包含错误的回溯,因此很难得出问题的原因。 我确实注意到您没有考虑

GET
函数中的
login
方法。

我建议尝试这个修改后的代码版本:

@app.route("/login", methods=["GET", "POST"])
def login():
    if request.method == 'POST':
        username = request.form["Username"]
        password = request.form["Password"]
        check_user = Users.query.filter_by(username=username).first()
        if check_user and check_user.check_password(password):
            session["username"] = username
            return redirect(url_for("dashboard"))
        else:
            return render_template('login.html', error="Invalid username or password")
    else:
        # If the request method is GET, just render the login page
        return render_template('login.html')
© www.soinside.com 2019 - 2024. All rights reserved.