在同一页面Flask项目中更新和显示数据

时间:2016-08-27 00:34:56

标签: python-2.7 flask flask-sqlalchemy flask-wtforms flask-security

我对如何实现这一点感到困惑。基本上,我有edit_profile页面,登录后,用户被重定向到此页面以填写他的个人资料。用户可以填写他的数据,点击保存,然后将数据保存在MySQL中,并返回到同一个edit_profile,所有字段都从数据库中填写。

@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)

我的问题是,这是正确的方法吗?

目前,数据已保存在SQLite中,但随后显示了表单,我看到空文本框。

表单保存后我是否正确传递了值?

如何在保存后传递值?

我可以将其显示为{{first_name}}

这么简单

我的模特是:

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)

当我从sqlite查询时:

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

我只是得到了我在 repr 中提到的,即first_name和last_name。如何获取所有字段以及如何在表单中显示此数据??

这不起作用:

   <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>

0 个答案:

没有答案