0

I am getting confused on how to implement 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 SQLite, 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)

When I query from sqlite like :

CustProfileTB.query.filter_by(cust_id=4).first()

I just get what I mentioned in repr which is first_name and last_name. How do I get all the fields and how do I display this data in the form...?

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>
JPro
  • 6,292
  • 13
  • 57
  • 83
  • Right before you render the template you can do one of two things: you can pass in values to the form by doing this `custprofileform.first_name.data = profiledata.first_name` and it will show the default value. Alternatively (and better) you could pass the database object to the form: `custprofileform = CustProfileForm(obj=profiledata)` but to use that method, the form fields and the db.model fields need to match. – Dan Safee Aug 27 '16 at 01:21
  • almost you have done. just convert your query result to a dict object before render the template – SumanKalyan Aug 27 '16 at 05:18
  • And use {{ custprofileform.first_name }} instead of {{ first_name }} – SumanKalyan Aug 27 '16 at 05:25
  • Thank you both, I fixed initial form POST with this: db.session.add(profiledata) db.session.commit() custprofiledata = CustProfileForm(obj=profiledata) flash("Customer profile saved successfully") print('DEBUG=>POST=> custprofiledata is :', custprofiledata, file=sys.stderr) return render_template("edit_profile.html", custprofileform=custprofiledata, post_result=True) But, when I update the form , it does not update. I think I am not passing in the correct data – JPro Aug 27 '16 at 10:40
  • I think I fixed the update as well, but I get this error : {{ custprofileform.custsubmit(class="btn btn-info") }} UndefinedError: '__main__.CustProfileTB object' has no attribute 'custsubmit' This is for SUbmit button. Not sure how to pass this to form... – JPro Aug 27 '16 at 11:10
  • Ok. I fixed that as well. – JPro Aug 27 '16 at 11:20

0 Answers0