如何让每个联系人成为链接?烧瓶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