2014-01-30 102 views
1

當我填寫表格,我得到這個錯誤,我沒有看到閃光燈的消息,儘管它把它添加到我的分貝:瓶生成錯誤

werkzeug.routing.BuildError 
BuildError: ('/contacts', {}, None) 

我的方法:

@app.route('/contacts/', methods=['GET','POST']) 
def contact_list(): 
    cur = g.db.execute('select contact_id, surname, firstname from address order by surname') 
    contacts = cur.fetchall() 
    return render_template("contacts.html", contacts = contacts) #refer to template 


@app.route('/addcontact/', methods=['GET','POST']) 
def contact_add(): 
    if request.method == 'POST': 
     g.db.execute('insert into address (surname, firstname, email, mobile) values (?, ?, ?, ?)', 
       [request.form['firstname'], request.form['surname'], request.form['email'] 
       , request.form['mobile']]) 
     g.db.commit() 
     flash('New entry was successfully posted') 
     return redirect(url_for('/contacts')) #redirect to the contacts page 
    elif request.method != 'POST': 
     return render_template('addcontact.html') 

我的HTML :

<html> 
    <body> 
     <h1>Add new contact</h1> 
    <form action='/addcontact/' method="post"> 
    <dl> 
     <dt>First Name: 
     <dd><input type="text" size=30 name="firstname"> 
     <dt>Surname: 
     <dd><input type="text" size=30 name="surname"> 
     <dt>Email: 
     <dd><input type="text" size=30 name="email"> 
     <dt>Mobile: 
     <dd><input type="text" size=30 name="mobile">    
     <dd><input type="submit" value="Add New Contact"> 
    </dl> 
    </form> 
    <a href="/">Home</a> 
    <a href="/contact">List of contacts</a> 

回答

2

url_for()呼叫失敗;你需要給它的路線名稱,而不是網址。因爲沒有/contacts路線,所以引發例外。這應該工作:

url_for('contact_list') 

因爲你叫g.db.commit()新的聯繫人已經被url_for()調用引發異常的時間提交到數據庫。