Hi,
I am getting confused on how to solve this. Basically, I have edit_profile page where after login, user gets redirected to this page to fill in his profile. User can fill in his data, click save and then it saves the data in MySQL and comes back to the same edit_profile with all the fields fill in from database.
@app.route("/edit_profile", methods=['GET','POST'])
@login_required
def edit_profile():
custprofileform = CustProfileForm()
if request.method == "POST" and custprofileform.validate_on_submit():
get_user_profile = CustProfileTB.query.filter_by(cust_id = current_user.id).first()
if get_user_profile is None:
profiledata = CustProfileTB(first_name=custprofileform.first_name.data,
last_name= custprofileform.last_name.data,
first_line_address=custprofileform.first_line_address.data,
second_line_address=custprofileform.second_line_address.data,
postcode=custprofileform.phone.data,
phone=custprofileform.phone.data,
agree_tos=custprofileform.agree_tos.data,
cust_id=current_user.id)
db.session.add(profiledata)
db.session.commit()
flash("Customer profile saved successfully")
return render_template("edit_profile.html", first_name=custprofileform.first_name.data)
else:
#profile already exists, so update fileds
profiledata = CustProfileTB(obj=get_user_profile)
db.session.commit()
return render_template("edit_profile.html", first_name=custprofileform.first_name.data)
if request.method == 'GET':
data_to_populate = CustProfileTB.query.filter_by(cust_id=current_user.id).first()
if data_to_populate:
return render_template("edit_profile.html", custprofileform=data_to_populate)
return render_template("edit_profile.html", custprofileform=custprofileform)
my question is, is this correct way to do it?
At the moment, data gets saved in MySQL, but then the form is shown, I see empty textboxes.
Am I passing in the values correctly after form save?
How do I pass values after save?
Can I show it as simple as {{ first_name }}
my Model is :
class CustProfileTB(db.Model):
id = db.Column(db.Integer(), primary_key=True)
first_name = db.Column(db.String(50))
last_name = db.Column(db.String(50))
first_line_address = db.Column(db.String(255))
second_line_address = db.Column(db.String(255))
postcode = db.Column(db.String(255))
phone = db.Column(db.String(20))
agree_tos = db.Column(db.Boolean(), default=False)
cust_id = db.Column(db.Integer())
def __init__(self, first_name=None, last_name=None, first_line_address=None, second_line_address=None, postcode=None, phone=None, agree_tos=None, cust_id=None ):
self.first_name = first_name
self.last_name = last_name
self.first_line_address = first_line_address
self.second_line_address = second_line_address
self.postcode = postcode
self.phone = phone
self.agree_tos = agree_tos
self.cust_id = cust_id
def __repr__(self):
self.res = self.first_name+" "+self.last_name
return '<CustProfileTB {self.res}>'.format(self=self)
and this does not work:
<label class="col-lg-3 control-label">First Name</label>
<div class="col-lg-8">
{{ custprofileform.csrf_token }}
{% if not first_name %}
<input type="text" name="first_name" placeholder="Insert First Name" data-required="true" class="form-control">
{% else %}
<input type="text" name="{{ first_name }}" placeholder="Insert First Name" data-required="true" class="form-control">
{% endif %}
</div>
Thanks.