2014-01-30 158 views
0

如何讓每個聯繫人成爲鏈接?燒瓶HTML鏈接查詢

我在想這樣做:

<li><h3><a href{url_for('addcontact/contact_id"{{e[1].encode("utf-8")}}, {{e[2].encode("utf-8")}}" {contact_id}</a></h3></li>

我的HTML

<html> 
    <body> 
     <h1>List of contacts</h1> 
     <ul class=contacts> 
    {% for e in contacts %} 
    <li><h3>"{{e[1].encode("utf-8")}}, {{e[2].encode("utf-8")}}"></h3></li> 
    {% else %} 
    <li><em>No contacts available</em></li> 
    {% endfor %} 
    </ul> 
    <a href="/">Home</a> 
    <a href="/addcontact/">Add a contact</a> 

我的方法: 接觸方法

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

` 附加說明 基本上我想顯示姓氏和名字,但通過CONTACT_ID當你點擊進入該鏈接,進入細節接觸。

聯繫詳細方法:聯繫人的

詳細

@app.route('/contacts/<int:contact_id>', methods=['GET']) 
def contact_detail(contact_id): 
    if request.method == 'GET': 
     cur = g.db.execute('select surname, firstname, email, mobile\ 
         from address where contact_id = ?', [contact_id]) 
     select = cur.fetchall() 
     return render_template('modcontact.html', select=select, contact_id=contact_id) 

詳細聯繫人模板:

<html> 
<body> 
    <h1>Detail of contact {{select[0]}}</h1> 
    <form action='/addcontact/' method="post"> 
    <dl>  
     <dd><input type="submit" value="Modify Contact"> 
     <dd><input type="submit" value="Delete Contact">  
    </dl> 
    </form> 
    <a href="/">Home</a> 
    <a href="/contacts">List of contacts</a> 
    </body> 
</html> 
+1

模板代碼中有幾處語法錯誤。此外,它看起來好像你誤解了'url_for()'的使用。你想鏈接到什麼'href'? –

+0

是的,我有一種感覺他們錯了。我假設href應該引用url_for? href引用其他方法。聯繫人和問候頁面 – Lorbat

回答

2

看起來像有與當前的方法的兩個問題。

首先,您使用url_for()端點不是路由,所以假設你有一個函數

@app.route('/contacts') 
def contact_list(): 
    return '' 

爲了得到一個模板中這個網址,你可以使用:

url_for('contact_list') 

不是

url_for('/contacts') 

第二個,它看起來像你逃跑了兩次,這是你不需要做的。嘗試:

<li><h3> 
    <a href="{{ url_for('the_endpoint_Im_trying_to_reach', contact_id=contact_id_field) }}"> 
     {{ first_name_field.encode('utf-8') }}, {{ surname_field.encode('utf-8') }} 
    </a> 
</h3></li> 

更新:爲了關鍵字參數,以獲得在url_for()路線過去了,你需要定義您的路線,例如:

@app.route('/contacts/<contact_id>') 
def the_endpoint_Im_trying_to_reach(contact_id=None): 
    pass 

url_for一些更多的信息可以發現, here

+0

基本上我想顯示姓氏和名字,但通過contact_id當你點擊鏈接,而不是顯示contact_id – Lorbat

+0

@Lorbat:固定。順便說一下,您發佈的視圖函數中沒有一個接受「contact_id」參數。 –

+0

雖然地址看起來像這樣:/ contacts /?contact_id =雖然我需要它是/ contacts/1或/ contacts/2 – Lorbat