2014-01-30 157 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