zoukankan      html  css  js  c++  java
  • 完成评论功能

      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
      

        

  • 相关阅读:
    可变长参数
    函数的参数
    函数
    文件操作
    字符编码
    数据类型的内置方法:集合
    数据类型的内置方法:元组
    数据类型的内置方法:字典
    数据类型内置方法:列表
    js对对象引用
  • 原文地址:https://www.cnblogs.com/YyYyYy11/p/8004148.html
Copyright © 2011-2022 走看看