我目前正在开发一个 Flask 应用程序,之前在另一个应用程序中使用的身份验证代码出现了问题。该代码在
我目前正在开发一个 Flask 应用程序,我遇到了之前在另一个应用程序中使用的身份验证代码的问题。该代码在原始项目中运行正常,但当我将其复制到新项目时,我遇到了一个错误,指出它无法在数据库中找到用户名,即使它确实存在。python 3.12如何修复它?app.py
from flask import Flask
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_migrate import Migrate
from config import Config
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
bcrypt = Bcrypt()
login_manager = LoginManager()
login_manager.login_view = 'auth.login' # Указываем представление для входа
login_manager.login_message = 'Please log in to access this page.'
migrate = Migrate()
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)
db.init_app(app)
bcrypt.init_app(app)
login_manager.init_app(app)
migrate.init_app(app, db)
from models import User
from routes import main_routes, user_routes, admin_routes, article_routes, meeting_routes, test_routes, \
favorite_routes, bookmarks_routes,auth_routes
app.register_blueprint(main_routes)
app.register_blueprint(user_routes)
app.register_blueprint(admin_routes)
app.register_blueprint(article_routes)
app.register_blueprint(meeting_routes)
app.register_blueprint(test_routes)
app.register_blueprint(favorite_routes)
app.register_blueprint(bookmarks_routes)
app.register_blueprint(auth_routes)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
return app
模型.py
class User(db.Model):
__tablename__ = 'users'
@property
def is_active(self):
# Assuming all users are active unless specified otherwise
return True
@property
def is_authenticated(self):
return True # You might want to adjust this logic based on your session management
@property
def is_active(self):
return True # Already added, but ensure it's correct
@property
def is_anonymous(self):
return False # Typically false for authenticated users
def get_id(self):
return str(self.id)
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(150), index=True, unique=True) # Изменено на username
email = db.Column(db.String(120), unique=True, nullable=False)
password_hash = db.Column(db.String(128), nullable=False)
role = db.Column(db.String(50), nullable=False, default='user') # Новое поле для роли
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
articles = db.relationship('Article', backref='author', lazy=True)
favorite_articles = db.relationship('Article', secondary='favorites', backref='favorited_by')
bookmarked_articles = db.relationship('Article', secondary='bookmarks', backref='bookmarked_by')
meetings = db.relationship('Meeting', backref='creator', lazy=True)
def set_password(self, password_hash):
self.password_hash = generate_password_hash(password_hash)
def check_password(self, password_hash):
return check_password_hash(self.password_hash, password_hash)
def __repr__(self):
return '<User {}>'.format(self.username)
验证码
@auth_routes.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('main.index'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user and user.check_password(form.password.data):
login_user(user, remember=True)
next_page = request.args.get('next')
return redirect(next_page or url_for('main.index'))
else:
flash('Invalid username or password')
return render_template('login.html', title='Sign In', form=form)
以下是有关该情况的一些详细信息: