完成評論功能

    1. 定義評論的視圖函數
      @app.route('/comment/',methods=['POST'])
      def comment():
      讀取前端頁面數據,保存到數據庫中
    2. 用<input type="hidden" 方法獲取前端的"question_id" 
    3. 顯示評論次數
    4. 要求評論前登陸
    5. 嘗試實現詳情頁面下的評論列表顯示

視圖函數:css

@app.route('/xiangqing/<question_id>')
def xiangqing(question_id):
        quest = Question.query.filter(Question.id == question_id).first()
        return render_template('xiangqing.html',ques=quest)

@app.route('/comment/',methods=['POST'])
@denglufirst
def comment():
    comment=request.form.get('pinglun')
    ques_id=request.form.get('question_id')
    user = User.query.filter(User.username == session.get('user')).first()
    auth_id=user.id
    comm=Comment(author_id=auth_id,question_id=ques_id,detail=comment)
    db.session.add(comm)
    db.session.commit()
    return redirect(url_for('xiangqing',question_id=ques_id))

詳情頁面htmlhtml

{% extends'shouye.html' %}
{% block title %}
問答詳情頁
{% endblock %}
{% block jiemian %}
<body bgcolor="antiquewhite"><link rel="stylesheet" type="text/css" href="../static/css/cc.css">
    <div class="d1">
        <form action="{{ url_for('comment') }}" method="post">
            <p><span class="timu">{{ ques.title }}<br></span>
            <h1 class="neir" >{{ ques.detail }}</h1></p>
            <a href="#" class="a"><img class="people" src="{{ url_for('static',filename='image/people.png') }}">{{ ques.author.username }}</a>
            <hr class="hr">
            <textarea class="pinglun" name='pinglun'></textarea>
            <input name="question_id" type="hidden" value="{{ ques.id }}">
            <br> <input  type="submit" value="發佈"style="width:65px;height:25px;font-size:15px">
            <p>評論:({{ ques.comments|length }})</p>
            <table border=1 >
                {% for foo in ques.comments %}
                    <tr><td>
                        <img class="people" src="{{ url_for('static',filename='image/people.png') }}">
                    <a href="#" class="name">{{ foo.author.username }}</a>
                     <span class="time" >{{ foo.creat_time }}</span><br>
                    <p class="nr">{{ foo.detail }}</p></td></tr>
                {% endfor %}
            </table>
        </form>
   </div>
</body>
{% endblock %}
</html>

   

相關文章
相關標籤/搜索