完成評論功能 完成評論功能

1.定義評論的視圖函數
@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 = request.form.get('question_id')
        comment = Comment(author_id=author_id,question_id=question_id,detail=detail)
        db.session.add(comment)  # 保存到數據庫
        db.session.commit()  # 提交
        return redirect(url_for('detail',question_id=question_id))
複製代碼

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

3.顯示評論次數數據庫

4.要求評論前登陸session

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

{% extends 'base.html' %}
{% block title %}問答詳情{% endblock %}

{% block main %}
    <div class="page-header">
        <h3>Title {{ ques.title }}<br><small>{{ ques.author.username }} <span class="badge">{{ ques.creat_time }}</span></small>></h3>
    </div>
    <p class="lead">detail{{ ques.detail }}</p>
    <hr>

    <form action="{{ url_for('comment') }}"method="post" style="...">
        <div class="form-group">
            <textarea name="new_comment" id="new-comment" class="form-control" rows="3"
                      placeholder="write your comment"></textarea>
            <input type="hidden" name="question_id" value="{{ ques.id }}">
        </div>
        <button type="submit" class="btn btn-default">發送</button>
    </form>
    <h4>評論:({{ ques.comments|length }})</h4>>
    <ul class="list-group" style="margin:10px"></ul>>
{% endblock %}
相關文章
相關標籤/搜索