如何讓每個聯繫人成爲鏈接?燒瓶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>
模板代碼中有幾處語法錯誤。此外,它看起來好像你誤解了'url_for()'的使用。你想鏈接到什麼'href'? –
是的,我有一種感覺他們錯了。我假設href應該引用url_for? href引用其他方法。聯繫人和問候頁面 – Lorbat