以下代码应该是编辑个人资料页面的视图,当提交表单时,应用程序会检查并查找用户名或电子邮件是否已经存在,如果存在,它将引发 Integrity 异常。但该程序没有按预期工作。提交表单时,即使数据库中没有这样的电子邮件或用户名,它总是闪烁“用户名/电子邮件已注册”,为什么会这样?
@App.route("/edit_profile", methods=('GET', 'POST'))
def edit_profile():
global current_user
user_email = current_user
user = User.get(email=user_email.email)
username = user.username
email = user.email
form = LoginForm(username=user.username, email=user.email)
if request.method == "POST" :
if form.username.data != user.username :
try:
User.update(username=form.username.data).where(email==user.email).execute()
current_user = user
current_user.username = user.username
current_user.save()
except peewee.IntegrityError:
flash("Username already registered")
if form.email.data != user.email:
try :
User.update(email=form.email.data).where(username==user.username).execute()
current_user = user
current_user.email = user.email
current_user.save()
except peewee.IntegrityError :
flash("Email already registered")
if form.password.data != '':
User.update(password=security.generate_password_hash(form.password.data, method='pbkdf2:sha1', salt_length=8)).where(username==username).execute()
return render_template("edit_profile.html", form=form)