@app.route('/detail/<question_id>')
def detail(question_id):
quest =
return render_template('detail.html', ques = quest)
{{ ques.title}}
{{ ques.id }}{{ ques.creat_time }}
{{ ques.author.username }}
{{ ques.detail }}
創建評論的對象關係映射:html
class Comment(db.Model):
__tablename__='comment'app
嘗試實現發佈評論。函數
1.主PY文件寫視圖函數,帶id參數。 post
1 # 詳情頁 2 @app.route('/detail/<question_id>') 3 @loginFirst 4 def detail(question_id): 5 quest = Question.query.filter(Question.id == question_id).first() 6 comment = Comment.query.filter(Comment.question_id == question_id).order_by('-creat_time').all() 7 return render_template('detail.html', ques=quest,comment = comment)
2.首頁標題的標籤作帶參數的連接url
1 <div class="container"> 2 <div class="row clearfix"> 3 <div class="col-md-2 column"> 4 </div> 5 <div class="col-md-8 column"> 6 <ul class="list-group"> 7 <li class="list-group-item" style="width: 800px"> 8 <a class="wrap-img" href="#" target="_blank"> 9 <img src="http://www.bookmarkye.com/3.jpg" width="50px"> 10 </a> 11 <span class="glyphicon glyphicon-left" aria-hidden="true"></span> 12 <a href="#" target="_blank">{{ user }}</a> 13 <br> 14 <a href="#">{{ title }}</a> 15 <span class="badge">{{ time }}</span> 16 <p style="">{{ detail }} 17 </p> 18 </li> 19 {% for foo in questions %} 20 <li class="list-group-item" style="width: 800px"> 21 <a class="wrap-img" href="#" target="_blank"> 22 <img src="http://www.bookmarkye.com/3.jpg" width="50px"> 23 </a> 24 <span class="glyphicon glyphicon-left" aria-hidden="true"></span> 25 <a href="#" target="_blank">{{ foo.author.username }}</a> 26 <br> 27 <a href="{{ url_for('detail',question_id=foo.id) }}">{{ foo.title }}</a> 28 <span class="badge">{{ foo.creat_time }}</span> 29 <p style="">{{ foo.detail }} 30 </p> 31 {% endfor %} 32 </ul> 33 </div> 34 <div class="col-md-2 column"> 35 </div> 36 </div> 37 </div>
3.在詳情頁將數據的顯示在恰當的位置spa
1 <div class="container"> 2 <div class="row clearfix"> 3 <div class="col-md-2 column"> 4 </div> 5 <div class="col-md-8 column"> 6 7 <h3>{{ ques.title }}</h3> 8 <div style="padding: 20px;"> 9 {{ ques.detail }} 10 </div> 11 12 <hr> 13 <form class="form-horizontal" role="form" method="post" action="{{ url_for('answer') }}"> 14 <div class="form-group"> 15 <label for="inputEmail3" class="col-sm-2 control-label">評論內容</label> 16 <div class="col-sm-10"> 17 <input type="text" name="author_id" value="{{ user.id }}" hidden> 18 <input type="text" name="question_id" value="{{ ques.id }}" hidden> 19 <textarea class="form-control" name="detail" rows="10"></textarea> 20 </div> 21 </div> 22 <div class="form-group"> 23 <div class="col-sm-offset-2 col-sm-10"> 24 <button type="submit" class="btn btn-default">發佈</button> 25 </div> 26 </div> 27 </form> 28 <hr> 29 <h1>用戶評論</h1><br> 30 <ul> 31 {% for com in comment %} 32 <li class="list-group-item"> 33 <h4>{{ com.author.username }}</h4> 34 <div> 35 {{ com.detail }} 36 </div> 37 </li> 38 {% endfor %} 39 </ul> 40 </div> 41 <div class="col-md-2 column"> 42 </div> 43 </div> 44 </div>
4.創建評論的對象關係映射code
1 class Comment(db.Model): 2 __tablename__ = 'comment' 3 id = db.Column(db.Integer, primary_key=True, autoincrement=True) 4 author_id = db.Column(db.Integer, db.ForeignKey('user.id')) 5 question_id = db.Column(db.Integer, db.ForeignKey('question.id')) 6 creat_time = db.Column(db.DateTime, default=datetime.now()) 7 detail = db.Column(db.TEXT, nullable=False) 8 question = db.relationship('Question', backref=db.backref('comment')) 9 author = db.relationship('User', backref=db.backref('comment'))
5. 嘗試實現發佈評論orm
發佈前: htm
發佈後:對象