12.8

 

 

    1. 定義評論的視圖函數
      @app.route('/comment/',methods=['POST'])
      def comment():
      讀取前端頁面數據,保存到數據庫中
    2. 用<input type="hidden" 方法獲取前端的"question_id" 
    3. 顯示評論次數
    4. 要求評論前登陸
    5. 嘗試實現詳情頁面下的評論列表顯示
      @app.route('/comments/',methods=['POST'])
      @loginFirst
      def comments():
          comment=request.form.get('new_comment')
          questions_id =request.form.get('questions_id')
          author_id = User.query.filter(User.username == session.get('user')).first().id
          comments = Comment(detail=comment, author_id=author_id, questions_id=questions_id)
          db.session.add(comments)  
          db.session.commit() # 提交
          return redirect(url_for('detail',questions_id=questions_id))

       

{% extends'base.html' %}
{% blook title %}問答詳情{% endblock %}
{% block main %}
   <div class="page-header"
        <h3>{{ ques.title }}<br><small>{{ ques.author.username }}<span class="badge">{{ ques.creat_time}} </span></small></h3>
</div>
<p class="lead">{{ ques.detail }}</p>
<hr>
<form action="{{ url_for('comment') }}"method="''post"style=""></form>
<div class="form-group" style="align-items: ">
     <textarea name="new_comment" class="form-control" rows="3" id="new-comment" placeholder="write your comment"></textarea>
 <input type="hidden" name="questions_id" value="{{ questions.id }}">
</div> 
<button type="submit" class="btn btn-default">發送</button> </form>
<h4> 評論:({{ ques.comments|length }})</h4>
<ul class="list-group" style="align-items:"> </ul>

{% endblock %}
相關文章
相關標籤/搜索