how to pre populate field flask wtforms

Solutions on MaxInterview for how to pre populate field flask wtforms by the best coders in the world

showing results for - "how to pre populate field flask wtforms"
Giulio
11 Oct 2019
1@decorator_authorized_user  # This decorator should make sure the user is authorized, like @login_required from flask-login
2def editprofile(nickname = None):
3    # Prepare the form and user
4    form = EditProfile()
5    form_action = url_for('profile.editprofile')
6    my_user = Users.get(...)  # get your user object or whatever you need
7    if request.method == 'GET':
8        form.username.data = my_user.username
9        form.email.data = my_user.email
10        # and on
11    if form.validate_on_submit():
12        # This section needs to be reworked.
13        # You'll want to take the user object and set the appropriate attributes
14        # to the appropriate values from the form.
15        if form.username.data == nickname: 
16            query = EditProfile(form.username.data,
17                                form.email.data,
18                                form.about.data,
19                                form.website.data,
20                                )
21            print query #debug
22            db.session.add(query)
23            db.session.commit()
24            flash('User Updated')
25            print "added"
26            return(url_for('profile.editprofile'))
27    return render_template('profile/add.html', form=form,
28                           form_action=form_action, title="Update Profile")