完成评论功能

    1. 定义评论的视图函数
      @app.route('/comment/',methods=['POST'])
      def comment():
      读取前端页面数据,保存到数据库中
    2. 用<input type="hidden" 方法获取前端的"question_id" 
    3. 显示评论次数
    4. 要求评论前登录
    5. 尝试实现详情页面下的评论列表显示
      # 跳转发布详情
      @app.route('/fabuview/<fabu_id>')  # 和idea的update一样,将id带到控制器
      def fabuview(fabu_id):
          fa = Fabu.query.filter(Fabu.id == fabu_id).first()  # 根据id查询出整条元组记录,丢进fa
          comments = Comment.query.filter(Comment.fabu_id==fabu_id).all()
          return render_template('fabuview.html', fa=fa,comments=comments)  # 把值fa丢进键fa,在fabuview.html页面调用
      
      
      # 方法二:
      # fabu={
      # 'fa':Fabu.query.filter(Fabu.id == fabu_id).first()
      # }
      # return render_template('fabuview.html',**fabu)
      
      
      @app.route('/comment/',methods=['POST'])
      @loginFirst
      def comment():
          detail = request.form.get('pinglun')
          author_id = User.query.filter(User.username == session.get('user')).first().id
          fabu_id = request.form.get('fa_id')
          comment = Comment(detail=detail,author_id=author_id,fabu_id=fabu_id)
          db.session.add(comment)  # 执行操作
          db.session.commit()  # 提交到数据库
          return redirect(url_for('fabuview',fabu_id=fabu_id))
      {% extends 'daohang.html' %}
      {% block fabu_viewtitle %}发布内容{% endblock %}
      {% block fabu_viewhead %}
          <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
          <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
          <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      {% endblock %}
      {% block daohangbody %}
          <div class="col-md-2 column "></div>
          <div class="col-md-8 column ">
              <h2 href="#" class="text-center">{{ fa.title }}</h2>
              <br>
              <p class="text-center">
                  <a href="#">
                      <small>{{ fa.author.username }}</small>
                  </a>&nbsp&nbsp&nbsp
                  <span class="pull-center"><small>{{ fa.creat_time }}</small></span>
              </p>
              <hr>
              <p>{{ fa.detail }}</p>
              <hr>
      
              <form action="{{ url_for('comment') }}" method="post">
                  <div class="form-group">
                          <textarea name="pinglun" class="form-control" rows="5" id="pinglun"
                                    placeholder="请输入评论"></textarea>
                      <input type="hidden" name="fa_id" value="{{ fa.id }}">
                  </div>
                  <button type="submit" class="btn btn-default">发送</button>
                  <br>
                  <br>
                  <h4>评论:({{ fa.comments|length }})</h4>
                  <ul class="list-unstyled">
                      {% for foo in comments %}
                          <li class="list-group-item">
                              <a>{{ foo.author.username }}</a>
                              <span class="badge pull-right">{{ foo.creat_time }}</span>
                              <p>{{ foo.detail }}</p>
                              <br>
                          </li>
                      {% endfor %}
                  </ul>
              </form>
              <br>
              <br>
              <br>
      
          </div>
          <div class="col-md-2 column "></div>
      {% endblock %}

       

posted on 2017-12-08 21:06  152陈斯璐  阅读(126)  评论(0编辑  收藏  举报