完成評論功能

定義評論的視圖函數
@app.route('/comment/',methods=['POST'])
def comment():
讀取前端頁面數據,保存到數據庫中html

@app.route('/comment/', methods=['POST'])
@loginFirst
def comment():
    detail = request.form.get('detail')
    author_id = User.query.filter(User.username == session.get('user')).first().id
    question_id = Question.query.filter(User.username == session.get('user')).first().id
    comment = Comment(detail=detail, author_id=author_id, question_id=question_id)
    db.session.add(comment)
    db.session.commit()
    context = {
        'comments': Comment.query.order_by('create_time').all()
    }
    question = Question.query.filter(Question.id == question_id).first()
    return render_template('detail.html', ques=question, **context)

  

用<input type="hidden" 方法獲取前端的"question_id" 前端

 答:我用了別的方法,沒弄這個python

 

顯示評論次數數據庫

<h4>評論:({{ ques.comment|length }})</h4>

  

要求評論前登陸session

@loginFirst

  

嘗試實現詳情頁面下的評論列表顯示app

      <ul class="list-group">
                            {% for each in comments %}
                            <li class="list-group-item" >
                            <a  class="author" href="">{{ each.author.username }}</a>
                            <br>
                                <br>
                                <span >{{ each.create_time }}</span>
                        <p class="abstract">{{ each.detail }}</p>
                            </li>
                            {% endfor %}
                        </ul>

 

相關文章
相關標籤/搜索