2014-12-27 20 views
0

我正在使用燒瓶並且有一個包含從表中獲取的用戶名(user.html)的html頁面現在,我怎樣才能通過單擊每個用戶來查看更多詳細信息(哪個路由到配置文件)? 我不使用登錄應用程序,所以我不想用摹通過主鍵在燒瓶中獲取用戶

app.py

# am I doing it right? 
    @app.route('/profile/<int:id>') 
    def profile(id=None): 
    detail = Contacts.query.get(id) 
     return render_template('profile.html', detail= detail , id=id) 

user.html

{% extends "layout.html" %} 
{% block content %} 
    <h2>show user</h2> 
     {% for contact in contact %} 

      # I got error when I click on each user name to see their 'profile' 
      #I guess because of id How can Solve it? 
      #error BuildError: ('profile', {}, None) 
      <strong>name:</strong><a href={{url_for('profile')}}> 
      {{ contact.name}}</a><br> 

      {% endfor %} 
     {% endblock %} 

profile.html

{% extends "layout.html" %} 
{% block content %} 
    <h2>show user profile</h2> 
    # how can I make it specific for each row of table(each user)? 
     {% for detail in Contacts %} 
      <strong>name:</strong> {{ detail.name}} <br> 
      <strong>email:</strong> {{ detail.email }} <br> 
      <strong>age:</strong> {{ detail.age}} <br> 
      <br> 
      {% endfor %} 

    {% endblock %} 

model.py

class Contacts(db.Model): 
    __tablename__ = "Contacts" 
    id = db.Column(db.Integer, primary_key = True) 
    name = db.Column(db.String(50)) 
    email = db.Column(db.String(50)) 
    age = db.Column(db.Integer) 
    submit = SubmitField("Submit") 

回答

0

我注意到在你的代碼兩件事情:

# this 
<a href={{url_for('profile')}}> 

# should be 
<a href={{url_for('profile', id=contact.id)}}> 

# otherwise Flask can't find the route, because it needs an id 

而另外一個:

{% for detail in Contacts %} 

在你的模板中沒有這樣的東西,因爲你的視圖函數不會發送它的Contacts變量。只需擺脫循環並直接使用detail,因爲這是您發送給模板的內容。

+0

@琳達對不起。我更新了我的答案,以考慮這一點。希望能幫助到你。 – Jivan 2014-12-27 19:49:19

+0

謝謝你的幫助:) @Jivan – Linda 2014-12-27 19:52:18