一、主PY文件寫視圖函數,帶id參數。 html
@app.route('/detail/<question_id>')
def detail(question_id):
quest =
return render_template('detail.html', ques = quest)
def detailpage(question_id): quest=Question.query.filter(Question.id==question_id).first() return render_template('detailpage.html',ques=quest)
二、首頁標題的標籤作帶參數的連接。app
{{ url_for('detail',question_id = foo.id) }}函數
<ul class="list-group" style="margin-left:20%;margin-top: 5%;margin-right:20%"> {# <li class="list-group-item">#} {# <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>#} {# <a href="{{ url_for('detail')}}">username</a>#} {# <br>#} {# <a href="#)"title></a>#} {# <span class="badge">create_time</span>#} {# <p>detauk</p>#} {% for b in question %} <li class="list-group-item" > <a href={{ url_for('detail')}}>{{ b.author.username }}</a> <span class="badge">{{ b.creat_time }}</span> <p class="abstract">{{ b.detail }}</p> </li> {% endfor %} </ul>
三、在詳情頁將數據的顯示在恰當的位置。 post
{{ ques.title}}
{{ ques.id }}{{ ques.creat_time }}
{{ ques.author.username }}
{{ ques.detail }}
<div align="center"> <div class="a1"> <h3>{{ ques.title }}<br><small>{{ ques.author.Username }} <span class="badge">{{ ques.creat_time }}</span></small></h3></div> <p class="lead">{{ ques.detail }}</p> <hr> <form action="{{ url_for('comment') }}" method="post"> <h4>評論:({{ ques.comments|lenght }})</h4> </form> {# <h2>Beyond樂隊</h2>#} {# <p>Beyond</p>#} {# <p>1983年成立</p>#} {# <p>detail</p>#} <br></div> <div class="a2"> <div class="pinglun"><label for="comment">評論</label><br></div> <textarea id="comment" rows="12" name="commentdetail"></textarea></div> <input type="submit" class="fasong" value="發送評論"> </div>
四、創建評論的對象關係映射:url
class Comment(db.Model):spa
__tablename__='comment'code
class Comment(db.Model): __tablename__ = 'comment' id = db.Column(db.Integer, primary_key=True, autoincrement=True) author_id = db.Column(db.Integer,db.ForeignKey('user.id'))#外鍵 question_id=db.Column(db.Integer,db.ForeignKey('question.id'))#外鍵 detail = db.Column(db.Text, nullable=False) creat_time = db.Column(db.DateTime, default=datetime.now) question=db.relationship('Question',backref=db.backref('comments')) author = db.relationship('User', backref=db.backref('comments'))
五、 嘗試實現發佈評論。orm