我正在尝试不使用ORM登录用户。我的代码如下所示:
@app.route("/", methods=['GET', 'POST'])
def index():
if flask.request.method == 'POST':
email = request.form.get('email')
password = flask.request.values.get('password')
user = db.execute("SELECT * from users WHERE email = :email", {'email': email}).first()
if user is None or not check_password_hash(user._password, password):
flash("Invalid password or username")
return redirect(url_for('login'))
login_user(user, remember=True)
return render_template('index.html')
以及尝试登录后得到的信息:
AttributeError:无法在列'is_active'的行中找到列]
说出问题所在了:
login_user(user, remember=True)
此代码是否有问题,或者问题出在其他地方?
根据flask-login extension的文档,您的用户类必须实现,尤其是is_active
属性。
由于这些要求是相当通用的,因此您可以将UserMixin
中的flask_login
mixin用于您的User类。
from flask_login import UserMixin
class User(UserMixin, db.Model):
pass
您可以在此出色的教程中阅读有关它的更多信息:https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-v-user-logins