Fix profile editing form not coming pre-populated with data

Not sure why Form.populate_obj() wasn't working - looking at the source
and the description as well as relevant StackOverflow answers, that
method would apparently work just fine. Looking again, Form.process()
was recommended instead.

Also, I finally changed PyCharm to show me proper commit message
standards - so, be happy that things will look nice now. :)
This commit is contained in:
Xevion
2022-03-29 20:57:13 -05:00
parent 987e4f3256
commit 2cb42bbc8b

View File

@@ -73,17 +73,18 @@ def edit_user(username: str):
user = db.session.query(User).filter_by(username=username).first_or_404()
form = EditProfileForm(request.form)
if request.method == 'POST':
if form.validate():
if current_user.is_admin or current_user == user:
user.about_me = form.about_me.data
user.name = form.name.data
# Check that a form was submitted
if form.validate_on_submit():
# Check that the user submitting the form is allowed to do this
if current_user.is_admin or current_user == user:
user.about_me = form.about_me.data
user.name = form.name.data
db.session.commit()
return redirect(url_for('main.view_user', username=username))
db.session.commit()
return redirect(url_for('main.view_user', username=username))
return render_template('pages/user_edit.html', form=form)
form.populate_obj(user)
form.process(obj=user)
return render_template('pages/user_edit.html', form=form)