• 完成评论功能


      1. 定义评论的视图函数
        @app.route('/comment/',methods=['POST'])
        def comment():
        读取前端页面数据,保存到数据库中
      2. 用<input type="hidden" 方法获取前端的"question_id" 
      3. 显示评论次数
      4. 要求评论前登录
      5. 尝试实现详情页面下的评论列表显示
    1. @app.route('/comment/', methods=['POST'])
      @loginFrist
      def comment():
          comment_detail = request.form.get('new_comment')
          quest_id = request.form.get('question_id')
          author_id = User.query.filter(User.username == session.get('user')).first().id
          comment = Comment(detail=comment_detail, author_id=author_id, question_id=quest_id, )
          db.session.add(comment)
          db.session.commit()
          return redirect(url_for('detail', question_id=quest_id))
      

        

      <form role="form" action="{{ url_for('comment') }}" method="post">
                      <div class="form-group">
                          <label for="exampleInputEmail1">Write down your answer~~</label>
                          <textarea class="form-control" id="exampleInputEmail1" name="new_comment"></textarea>
                          <input type="hidden" name="question_id" value="{{ quest.id }}">
                      </div>
                      <button type="submit" class="btn btn-default">Submit</button>
                  </form>
      

        

      <h4>评论:({{ quest.comments|length }})</h4>
    2. # 装饰器
      def loginFrist(func):
          @wraps(func)
          def wrapper(*args, **kwargs):
              if session.get('user'):
                  return func(*args, **kwargs)
              else:
                  return redirect(url_for('login'))
          return wrapper
      

        

  • 相关阅读:
    Linux使用locate命令定位文件
    LINUX常用命令
    linux性能问题(CPU,内存,磁盘I/O,网络)
    Linux下常用的shell命令记录
    Linux下的进程管理
    Linux常用性能检测命令解释
    CentOS查看系统信息-CentOS查看命令
    linux系统中如何查看日志 (常用命令)
    美团HD(4)-二级联动效果
    美团HD(3)-加载分类导航数据
  • 原文地址:https://www.cnblogs.com/YyYyYy11/p/8004148.html
Copyright © 2020-2023  润新知