我正在使用 Flask+Python 并检查用户名(和电子邮件)是否已经被使用,我正在使用这个逻辑:
@app.route('/register', methods=['GET', 'POST'])
def register():
form = SignupForm()
if form.validate_on_submit():
user = Users.query.filter_by(username=form.username.data).first()
email = Users.query.filter_by(email=form.email.data).first()
if form.username.data in user:
error = 'Username already taken. Choose another'
elif form.email.data in email:
error = 'Email already registered. Login or register with another Email'
else:
user = Users(
form.username.data,
form.password.data,
#form.confirm.data ,
form.email.data,
1,
# form.cityaddress.data,
# form.countryaddress.data,
#form.accept_tos.data,
)
db.session.add(user)
db.session.commit()
return redirect(url_for('index'))
但它给出的错误就像object has no attribute 'username'
我知道我从 db 获取数据的逻辑不正确。我对 SQLalchemy 知之甚少。
你能建议我如何从表中获取Username
(和Email
)列值Users
,然后检查它们是否相同form.username.data
?