Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Edward Seibert
32,055 PointsPeewee and Flask, wtforms: How do I pull info from the db into a placeholder value?
I'm creating an app where people can edit their profile. I want the form on the profile edit page to display the current value for each field as the placeholder text (the 'value' attribute in the html). Here's what I've got on my forms file:
class ProfileForm(Form):
first_name = StringField(
'First Name',
validators=[
DataRequired(),
],
render_kw={"value": '{{ current_user.first_name }}' }
)
When I load up the form, instead of getting the user's first name displayed I'm seeing: '{{ current_user.first_name }}'. Flask is showing the string instead of the actual value.
How do I get the value from the db (it's sqlite, but will eventually migrate to mysql)?
1 Answer
Lenny Meyerson
15,993 Pointspull in the object to be edited and pass it to the form in the view. Something like this:
@user.route('/<int:user_id>/edit', methods=['GET', 'POST'])
def edit_user(user_id):
usr = User.query.get(user_id)
form = UserForm(obj=usr)
if request.method == 'POST' and form.validate_on_submit():
form.populate_obj(usr)
db.session.commit()
return redirect(url_for('.index'))
return render_template('user_form.html', form=form)