Reputation: 48
I'm trying to make the form for users to update their profile pictures auto-submit, whenever they put in a file. Here is my HTML code:
<form method="POST" action="" enctype="multipart/form-data">
{{ form.hidden_tag() }}
<div class="form-group">
<input type="file" accept=".png, .jpg, .jpeg, .gif" id="file" name="profile-picture" onchange="form.submit()">
</div>
</form>
My Python code:
@users.route("/account", methods=["POST", "GET"])
@users.route("/account/", methods=["POST", "GET"])
@login_required
def account():
form = UpdateAccountForm()
if form.validate_on_submit():
if request.files:
profile_picture = request.files["profile-picture"]
picture_file = save_picture(profile_picture) # save_picture is a function I wrote
current_user.image_file = picture_file
current_user.username = form.username.data
db.session.commit()
flash("Your profile has been updated!", "success")
return redirect(url_for("users.account"))
Normally, onchange="form.submit()" would work perfectly fine, but it doesn't seem to work when I'm using Python Flask. The form would submit if I put a submit button, but does not submit automatically. Does anyone know what I've done wrong here?
Upvotes: 0
Views: 540
Reputation: 81
I think you should write profile_picture = request.form["profile-picture"]
.
Upvotes: 0