I'm working on a flask app, and to change the profile picture I have two buttons, one for selecting the image from your files, and another to literally refresh the whole template in order to see the changes. I would like to know is there is another way to do this but when the user selects the file, the profile picture immediately changes without having to render the whole page.
This is how I'm doing it right now:
if request.method == "POST" and "photo" in request.files:
photo = request.form.get("photo")
user_id = session["user_id"]
filename = photos.save(request.files['photo'])
row = User.query.filter_by(user_id = user_id).first()
row.img_url = filename
updated_filename = row.img_url
db.session.commit()
An this is my html:
<form action="{{ url_for('profile') }}" method="post" enctype=multipart/form-data>
<p class="text-center text-muted">Aquí vas a poder editar tu información. Una vez que estes listo, presionas en "Ver" y aparecerá como se verá tu perfil cuando otras personas lo escaneen! </p>
{% if genre %}
<center><div class="circular"><img border="1" style="width:30%; object-fit: contain" class="rounded-circle" src="static/img/{{updated_filename}}"></div></center>
<br>