完成评论功能
2017-12-08 18:57 095罗其婷 阅读(152) 评论(0) 收藏 举报- 定义评论的视图函数
@app.route('/comment/',methods=['POST'])
def comment():
读取前端页面数据,保存到数据库中@app.route('/comment/', methods=['GET','POST']) @loginFirst def comment(): comment = request.form.get('new_comment') ques_id = request.form.get('question_id') auth_id = User.query.filter(User.username == session.get('user')).first().id comm = Comment(author_id = auth_id, question_id = ques_id, detail=comment) db.session.add(comm) db.session.commit() return redirect(url_for('detail')) - 用<input type="hidden" 方法获取前端的"question_id"
- 显示评论次数
- 要求评论前登录
@loginFirst
- 尝试实现详情页面下的评论列表显
{% extends 'base.html' %} {% block title %} 问答详情 {% endblock %} {% block main %} <div class="box"> <h3 href="#" >{{ ques.title }}</h3><small> {{ ques.author.username }}<span class="badge" style="margin-left: 75%">{{ ques.create_time }}</span></small> <hr> <p class="lead">{{ ques.detail }}</p> <hr> <form action="{{ url_for('comment') }}" method="post" style=""> <div class="form-group"> <input type="text" value="{{ ques.id }}" name="question_id" hidden> <input type="text" value="{{ user.id }}" name="author_id" hidden> <textarea name="detail" class="form-control" row="3" id="new-comment" placeholder="write your comment"></textarea> </div> <button type="submit" >发送</button> </form> <h4>评论({{ questions.comments|length }})</h4> <ul class="list-group"> <li class="list-group-item"> <img style="width: 30px" src="{{ url_for('static',filename='css/pink.jpg') }}" alt="64"> <a href="#"></a><br> <p style="align-content: center"></p> <span class="badge" style="margin-left: 60%"></span> <p style="margin-left: 25%"></p><br> </li> </ul> </div> {% endblock %}
浙公网安备 33010602011771号