我正在阅读Flask.Web.Development,并阅读如下代码:
@app.route('/', methods=['GET', 'POST'])
def index():
form = NameForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.name.data).first()
if user is None:
user = User(username = form.name.data)
db.session.add(user)
session['known'] = False
else:
session['known'] = True
session['name'] = form.name.data
form.name.data = ''
return redirect(url_for('index'))
return render_template('index.html',
form = form, name = session.get('name'), known = session.get('known', False))
第7行,为什么只是db.session.add(用户)而没有db.session.commit()? 它只是添加到会话但不提交到数据库。但数据库确实已更新。