def account(): form = UpdateAccountForm() user = User.query.filter_by(username=current_user.username).first() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash("Your account has been updated!", "success") return redirect(url_for("user.account")) elif request.method == "GET": form.username.data = current_user.username form.email.data = current_user.email image_file = url_for("static", filename=f"profile_pics/{current_user.image_file}") return render_template("account.html", image_file=image_file, form=form, legend="Update your Account")
def account(): form = UpdateAccountForm() if form.validate_on_submit(): # Since profile picture isn't necessary, add a check here to see if # anything was put in if form.picture.data: picture_fn = save_picture(form.picture.data) current_user.image_file = picture_fn current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash("Your details have been updated", "success") return redirect(url_for('user.account')) # this will be reached, if the user directly reached this page # as opposed to having reached it back by filling the form on it # that would have been a POST request. # We keep the user's current details prefilled. elif request.method == "GET": form.username.data = current_user.username form.email.data = current_user.email ''' It is a good idea, to do as much computation in your py files, and then send to your html as a parameter, instead of crowding your html with if blocks. You can send as many as you want after all. ''' image_src = url_for('static', filename='profilepics/' + current_user.image_file) return render_template("account.html", title=current_user.username, image_src=image_src, form=form)
def account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('Your account has been updated!', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title='Account', image_file=image_file, form=form)
def account(): form = UpdateAccountForm() if form.validate_on_submit(): # updating picture username and email if post if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('Your Account Has Been Updated','success') return redirect(url_for('users.account')) # if you want to show current data without req post method in the input field of account then elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email imagefile = url_for('static',filename='img/'+current_user.image_file) #image_file is the column name in User table check out models with the help of current user we can access to exact location if we dont then sql ll rise error return render_template('account.html',title='Account', imagefile=imagefile, form=form)