如何在Flask-Login中实现工作日志记录?

huangapple go评论107阅读模式
英文:

How to get working logging with flask-login?

问题

我正在使用Python开发一个应用程序,其中包括用户登录。我正在使用Flask以及SQLAlchemy、Flask-Login和Flask表单。以下是我的注册和登录代码(位于模块routes.py):

  1. @app.route('/register', methods=['GET', 'POST'])
  2. def register():
  3. register_form = RegistrationForm(request.form)
  4. if request.method == 'POST':
  5. if register_form.validate():
  6. email = request.form.get('email')
  7. password = request.form.get('password')
  8. username = request.form.get('username')
  9. existing_user = User.query.filter_by(email=email).first()
  10. if existing_user is None:
  11. user = User(username=username, email=email,
  12. password=bcrypt.generate_password_hash(password))
  13. db.session.add(user)
  14. db.session.commit()
  15. login_user(user)
  16. return redirect(url_for('home'))
  17. flash('A user already exists with that email address.')
  18. return redirect(url_for('register'))
  19. return render_template('/register.html',
  20. title='Create an Account',
  21. form=RegistrationForm())
  22. @app.route('/login', methods=['GET', 'POST'])
  23. def login():
  24. login_form = LoginForm(request.form)
  25. if request.method == 'POST':
  26. if login_form.validate():
  27. email = request.form.get('email')
  28. password = request.form.get('password')
  29. user = User.query.filter_by(email=email).first()
  30. if user:
  31. if user.check_password(password=password):
  32. login_user(user)
  33. next = request.args.get('next')
  34. return redirect(next or url_for('home'))
  35. flash('Invalid username/password combination')
  36. return redirect(url_for('login'))
  37. return render_template('login.html',
  38. form=LoginForm())

可以看到,它们以相似的方式编写。这是我的日志记录器函数(位于模块models.py):

  1. @login_manager.user_loader
  2. def load_user(user_id):
  3. return User.query.get(int(user_id))

注册功能按预期工作,但登录功能却不然。当我使用有效的电子邮件和密码(保存在SQL中工作正常)时,我的Web应用程序总是显示“无效的用户名/密码组合”。

有人可以提供帮助吗?

英文:

I'm developing an app in Python that includes logging users. I'm using flask incl. SQLAlchemy, flask-login and flask forms. These are my codes for registering and logging (in module routes.py):

  1. @app.route('/register', methods=['GET', 'POST'])
  2. def register():
  3. register_form = RegistrationForm(request.form)
  4. if request.method == 'POST':
  5. if register_form.validate():
  6. email = request.form.get('email')
  7. password = request.form.get('password')
  8. username = request.form.get('username')
  9. existing_user = User.query.filter_by(email=email).first()
  10. if existing_user is None:
  11. user = User(username=username, email=email,
  12. password=bcrypt.generate_password_hash(password))
  13. db.session.add(user)
  14. db.session.commit()
  15. login_user(user)
  16. return redirect(url_for('home'))
  17. flash('A user already exists with that email address.')
  18. return redirect(url_for('register'))
  19. return render_template('/register.html',
  20. title='Create an Account',
  21. form=RegistrationForm())
  22. @app.route('/login', methods=['GET', 'POST'])
  23. def login():
  24. login_form = LoginForm(request.form)
  25. if request.method == 'POST':
  26. if login_form.validate():
  27. email = request.form.get('email')
  28. password = request.form.get('password')
  29. user = User.query.filter_by(email=email).first()
  30. if user:
  31. if user.check_password(password=password):
  32. login_user(user)
  33. next = request.args.get('next')
  34. return redirect(next or url_for('home'))
  35. flash('Invalid username/password combination')
  36. return redirect(url_for('login'))
  37. return render_template('login.html',
  38. form=LoginForm())

As can be seen, they are written in a similar manner. This is my logger function (in module models.py):

  1. @login_manager.user_loader
  2. def load_user(user_id):
  3. return User.query.get(int(user_id))

Registering works as expected, but the same can't be said for logging in. When I use valid email and password (saving in sql works fine), my web app always flashes 'Invalid username/password combination'.

Can anyone help?

答案1

得分: 0

我找到了一个解决方案。我的登录表单如下所示:

  1. class LoginForm(FlaskForm):
  2. email = StringField('邮箱',
  3. validators=[DataRequired(), Email()])
  4. password = PasswordField('密码',
  5. validators=[DataRequired()])
  6. remember = BooleanField('记住我')
  7. submit = SubmitField('登录')

我复制了我的注册表单,忘记删除confirm_password字段。在登录时,它没有被确认,因此字段条件不满足 --> form.validate_on_submit为false。

英文:

I found a solution. My form for Login looked as follows:

  1. class LoginForm(FlaskForm):
  2. email = StringField('Email',
  3. validators=[DataRequired(), Email()])
  4. password = PasswordField('Password',
  5. validators=[DataRequired()])
  6. confirm_password = PasswordField(' Confirm Password',
  7. validators=[DataRequired(), EqualTo('password')])
  8. remember = BooleanField('Remember Me')
  9. submit = SubmitField('Log In')

I copied my Register form and forgot to delete confirm_password field. It is not being confirmed while logging in, so the field conditions is not satisfied --> form.validate_on_submit is false.

huangapple
  • 本文由 发表于 2020年1月6日 18:52:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定