从首页问答标题到问答详情页

    1. 主PY文件写视图函数,带id参数。 
      @app.route('/detail/<question_id>')
      def detail(question_id):
          quest = 
          return render_template('detail.html', ques = quest)

    2. 首页标题的标签做带参数的链接。
            {{ url_for('detail',question_id = foo.id) }}

    3. 在详情页将数据的显示在恰当的位置。 
      {{ ques.title}}
      {{ ques.id  }}{{  ques.creat_time }}
      {{ ques.author.username }} 
      {{ ques.detail }}
    4. 建立评论的对象关系映射:

      class Comment(db.Model):
          __tablename__='comment'

    5.  尝试实现发布评论。

1.主PY文件写视图函数,带id参数。 

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.首页标题的标签做带参数的链接

 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.在详情页将数据的显示在恰当的位置

 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.建立评论的对象关系映射

 

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. 尝试实现发布评论

  发布前:   

 

发布后:

  

 

posted @ 2017-12-05 16:05  064黄庚华  阅读(194)  评论(0编辑  收藏  举报