khushi
khushi

Reputation: 365

My html button doesn't update data in database (Flaskapp), what is wrong with my code?

So I am creating and working around forms in my web app. I have a profile page for all users (displaying their first name, last name, email, etc). I am trying to add an 'edit' button to it and once they click on it, they are taken to the update form page where they type in the new details and click on the update button and once they do so, they are taken back to their profile page but with the new details they have entered this time (a simple edit button)

my forms.py class for the update form is:

class Updateform(FlaskForm):
    email = StringField("Email", validators=[DataRequired(), Email()])
    fname = StringField("First Name", validators=[DataRequired(), Length(min=2, max=20)])
    lname = StringField("Last Name", validators=[DataRequired(), Length(min=2, max=20)])
    phoneno = StringField("Phone Number", validators=[DataRequired()])
    submit = SubmitField("Update account")

my route to the html is:

@auth.route("/updateform", methods=["GET", "POST"])
def updateform():
    form = Updateform()
    if(form.validate_on_submit()):
        user: User = User.select().where(User.email == form.email.data).first()
        user.email = form.email.data
        user.fname = form.fname.data
        user.lname = form.lname.data
        user.phoneno = form.phoneno
        user.save()
        flash('Your account has been updated', 'success')
        return redirect (url_for("user.profile"))
    return render_template("user/formupdate.html", form=form)

and here is the template for the update form((formupdate.html)):

{% extends "admin-layout.html" %}

{% block content %}
<div>
    {% from "_formhelpers.html" import render_field %}
    <div class="container">
        <div class="row">
            <div class="col-xs-12 col-sm-6 col-md-6">
                <div class="card shadow mb-4">
                    <div class="card-header py-3">
                        <h6 class="font-weight-bold text-primary">UpdateForm</h6>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-sm-6 col-md-8">
                                <div>
                                    {{ form.hidden_tag()}}
                                    {{ render_field(form.fname, class="form-control")}}
                                    {{ render_field(form.lname, class="form-control")}}
                                    {{ render_field(form.email, class="form-control")}}
                                    {{ render_field(form.phoneno, class="form-control")}}
                                
                                    <a href="{{ url_for('user.profile')}}" class="btn btn-primary">Update form</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>




{% endblock content %}

My problem is that when I click on the update button after entering new details, nothing changes - the page goes back to the profile page as it is supposed to, but the details are still the same and none of the new details are visible anywhere.

What am I doing wrong and how do I fix it?

Upvotes: 0

Views: 51

Answers (1)

oschlueter
oschlueter

Reputation: 2698

Check your implementation of user.save() for a call to commit the update to the database. Here is one of my routes for comparison, watch out for db.session.commit():

from flask import render_template, flash, redirect, url_for
from flask_login import login_required

from app import db
from app.artists import bp
from app.artists.forms import ArtistForm
from app.main.forms import EmptyForm
from app.models import Artist

@bp.route('/', methods=['GET', 'POST'])
@login_required
def overview():
    create_form = ArtistForm()
    delete_form = EmptyForm()
    if create_form.validate_on_submit():
        artist = Artist(name=create_form.name.data)
        artist.save()
        db.session.add(artist)
        db.session.commit()  # <-- persist data in database
        flash(f'Created artist {create_form.name.data}.')

        return redirect(url_for('artists.overview'))

    return render_template('artists/overview.html', artists=Artist.query.all(), create_form=create_form, delete_form=delete_form)

Upvotes: 1

Related Questions