gpt4 book ai didi

python-flask db.session.commit() 不起作用

转载 作者:行者123 更新时间:2023-12-01 04:47:08 26 4
gpt4 key购买 nike

问题是当用户尝试“忘记密码”选项时。它创建新的 reset_key 进行验证,但新 key 未更新到数据库中。

@app.route('/login/forgot/', methods=['GET', 'POST'])
def forgot():
form = ResetLoginForm(request.form)
#There's no session yet. User just pointing to /login/forgot url.

if request.method == 'POST' and form.validate():
user = User.query.filter_by(email=form.email.data).first()

if not user:
flash('The username or email incorrect')
return render_template('forgot.html', form=form)

reset_key = generate_key() ## this creates a new key, but how update this key into db?
#tried something like
user.reset_key = reset_key
db.session.add(user)
db.session.commit()
#this is not working. Is it due to session is not started or something?

感谢您的帮助或提示。

最佳答案

这是因为 User.query.filter_by(email=form.email.data).first() 将返回 sqlalchemy.orm.query.Query 对象。正如其文档所说:

Query is the source of all SELECT statements generated by the ORM, both those formulated by end-user query operations as well as by high level internal operations such as related collection loading. It features a generative interface whereby successive calls return a new Query object, a copy of the former with additional criteria and options associated with it.

所以你只是得到一个复制的对象,所以你的更改将不起作用;

你可以这样使用:user = db.session.query(User).filter_by(email==form.email.data).first()然后您可以更改用户属性

关于python-flask db.session.commit() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29194926/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com