2014-12-25 116 views
1

聖誕快樂!!!!我用燒瓶sqlalchemy和wtf工作。我可以創建新的,並將顯示在info.html但是,當我嘗試編輯表單沒有數據庫的變化所以它不工作。所以我想知道問題在哪裏?編輯表格不工作

app.py

#With this route I can add new form in database 
    @app.route('/contact', methods=['GET', 'POST']) 
    def contact(): 
     form = LoginForm(request.form) 
     if request.method == 'POST': 
      if form.validate()== True: 
       contact = Contacts() 
       # populate the model 
       form.populate_obj(contact) 
       db.session.add(contact) 
       db.session.commit() 
       # Contact commited to database 
       # RETURN TO INFO.HTML 
       return redirect(url_for('info')) 

      else: 
       #If the form does not have all fields that are required 
       return render_template('contact.html', form=form) 

    # This is the part for edit which is not working 
    # so I query and populate it but no change none in 
    # database none in info.html 
    @app.route('/edit/<int:id>', methods=['POST']) 
    def edit(id=None): 
     user = Contacts.query.get_or_404(id) 
     form = LoginForm(request.form,obj=user) 
     # check the validate and then populate the obj 
     if form.validate_on_submit()== True: 
      #populate it 
      form.populate_obj(user) 
      db.session.commit() 
      return redirect(url_for('info')) 
     else: 
      #If the form does not have all fields that are required 
      return render_template('edit.html', id=id) 
    @app.route('/edit/<int:id>', methods=['GET']) 
    def profile(id=None): 
    user = Contacts.query.get_or_404(id) 
    form = LoginForm(request.form, obj=user) 
    return render_template('edit.html',form=form, id =id) 
    # this route to html that should show all info 
    @app.route('/info', methods=['GET', 'POST']) 
    def info(): 
     #query all 
     info = Contacts.query.all() 
     return render_template('info.html', contact=info) 

model.py

# model with table name Contacts 
class Contacts(db.Model): 
     __tablename__ = "Contacts" 
     id = db.Column(db.Integer, primary_key = True) 
     name = db.Column(db.String(50)) 
     email = db.Column(db.String(50)) 
     age = db.Column(db.Integer) 

form.py

# this is the form wtf 
Class LoginForm(Form): 
     name = StringField("Name", [validators.Required("Please enter your name.")]) 
     email = StringField("Email", [validators.Required("Please enter your email address."), validators.Email("Please enter valid email address.")]) 
     age = IntegerField("age", [validators.Required("Please enter your age.")]) 
     submit = SubmitField("Submit") 

info.html裏

# it should display all updated form But it wont?? 
    {% extends "layout.html" %} 
    {% block content %} 
     <h2>show the info</h2> 
      {% for contact in contact %} # Maybe this is also Problem? 
       <strong>name:</strong> {{ contact.name}} <br> 
       <strong>email:</strong> {{ contact.email }} <br> 
       <strong>age:</strong> {{ contact.age}} <br> 
       <br> 
       {% endfor %} 

     {% endblock %} 
+0

仍然沒有很好的答案任何幫助嗎? – Linda

+0

是表單驗證?這樣你就可以到達用sqlalchemy真正處理事情的地方了? – muthan

+0

驗證不起作用時,您永遠不會更新。因爲更新僅在表單可能經過驗證時纔會發生。 – muthan

回答

0

您不必重新進行添加一個對象,當你得到它每次查詢。 Queryobject被綁定到會話。所以你只需要commit的更改,而不是add他們再次。

所以這應該是更正後的代碼片斷

user = Contacts.query.get_or_404(id) 
    form = LoginForm(request.form,obj=user) 
    # check the validate and then populate the obj 
    if form.validate()== True: 
     # populate it 
     form.populate_obj(user) 
     db.session.commit() 
+0

添加一個已經存在的實例沒有任何作用。誠然,這不是必要的,但它也沒有什麼壞處。 – davidism

+0

@muthan Thanx但不工作 – Linda

+0

@davidism你沒有變化,你能幫我解決這個問題嗎? Thanx – Linda

0

我想我的機器上的代碼。在代碼中幾乎沒有修改,我能夠更新數據庫。

請在下面找到更新塊:

@app.route('/edit/<int:id>', methods=['POST']) 
def submit(id=None): 
    user = Contacts.query.get_or_404(id) 
    form = LoginForm(request.form,obj=user) 
    #return render_template('edit.html', form=form, id=1) 
    if form.validate() == True: 
     form.populate_obj(user) 
     db.session.commit() 
     return redirect(url_for('info')) 
    else: 
     return redirect(url_for(edit, id=id)) 

@app.route('/edit/<int:id>', methods=['GET']) 
def edit(id=None): 
    user = Contacts.query.get_or_404(id) 
    form = LoginForm(request.form,obj=user) 
    return render_template('edit.html', form=form, id=id) 
+0

Thanx !!!不更新數據庫沒有更改@Deepak Kumar Mangal – Linda

+0

我嘗試你的新的數據庫但仍然沒有數據庫的變化任何想法?@ Deepak庫馬爾Mangal – Linda

+0

也許它沒有連接到數據庫不知道但我可以添加新的(路由聯繫人)正在工作所以不知道@Deepak庫馬爾曼加爾 – Linda