英文:
How to get working logging with flask-login?
问题
我正在使用Python开发一个应用程序,其中包括用户登录。我正在使用Flask以及SQLAlchemy、Flask-Login和Flask表单。以下是我的注册和登录代码(位于模块routes.py):
@app.route('/register', methods=['GET', 'POST'])
def register():
register_form = RegistrationForm(request.form)
if request.method == 'POST':
if register_form.validate():
email = request.form.get('email')
password = request.form.get('password')
username = request.form.get('username')
existing_user = User.query.filter_by(email=email).first()
if existing_user is None:
user = User(username=username, email=email,
password=bcrypt.generate_password_hash(password))
db.session.add(user)
db.session.commit()
login_user(user)
return redirect(url_for('home'))
flash('A user already exists with that email address.')
return redirect(url_for('register'))
return render_template('/register.html',
title='Create an Account',
form=RegistrationForm())
@app.route('/login', methods=['GET', 'POST'])
def login():
login_form = LoginForm(request.form)
if request.method == 'POST':
if login_form.validate():
email = request.form.get('email')
password = request.form.get('password')
user = User.query.filter_by(email=email).first()
if user:
if user.check_password(password=password):
login_user(user)
next = request.args.get('next')
return redirect(next or url_for('home'))
flash('Invalid username/password combination')
return redirect(url_for('login'))
return render_template('login.html',
form=LoginForm())
可以看到,它们以相似的方式编写。这是我的日志记录器函数(位于模块models.py):
@login_manager.user_loader
def load_user(user_id):
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):
@app.route('/register', methods=['GET', 'POST'])
def register():
register_form = RegistrationForm(request.form)
if request.method == 'POST':
if register_form.validate():
email = request.form.get('email')
password = request.form.get('password')
username = request.form.get('username')
existing_user = User.query.filter_by(email=email).first()
if existing_user is None:
user = User(username=username, email=email,
password=bcrypt.generate_password_hash(password))
db.session.add(user)
db.session.commit()
login_user(user)
return redirect(url_for('home'))
flash('A user already exists with that email address.')
return redirect(url_for('register'))
return render_template('/register.html',
title='Create an Account',
form=RegistrationForm())
@app.route('/login', methods=['GET', 'POST'])
def login():
login_form = LoginForm(request.form)
if request.method == 'POST':
if login_form.validate():
email = request.form.get('email')
password = request.form.get('password')
user = User.query.filter_by(email=email).first()
if user:
if user.check_password(password=password):
login_user(user)
next = request.args.get('next')
return redirect(next or url_for('home'))
flash('Invalid username/password combination')
return redirect(url_for('login'))
return render_template('login.html',
form=LoginForm())
As can be seen, they are written in a similar manner. This is my logger function (in module models.py):
@login_manager.user_loader
def load_user(user_id):
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
我找到了一个解决方案。我的登录表单如下所示:
class LoginForm(FlaskForm):
email = StringField('邮箱',
validators=[DataRequired(), Email()])
password = PasswordField('密码',
validators=[DataRequired()])
remember = BooleanField('记住我')
submit = SubmitField('登录')
我复制了我的注册表单,忘记删除confirm_password
字段。在登录时,它没有被确认,因此字段条件不满足 --> form.validate_on_submit
为false。
英文:
I found a solution. My form for Login looked as follows:
class LoginForm(FlaskForm):
email = StringField('Email',
validators=[DataRequired(), Email()])
password = PasswordField('Password',
validators=[DataRequired()])
confirm_password = PasswordField(' Confirm Password',
validators=[DataRequired(), EqualTo('password')])
remember = BooleanField('Remember Me')
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论