完成评论
Posted yishhaoo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完成评论相关的知识,希望对你有一定的参考价值。
- 定义评论的视图函数
@app.route(\'/comment/\',methods=[\'POST\'])
def comment():
py文件:
@app.route(\'/detail/<question_id>\') # 和idea的update一样,将id带到控制器 def detail(question_id): quest = Question.query.filter(Question.id==question_id).first()# 根据id查询出整条元组记录,丢进quest comments = Comment.query.filter(Comment.question_id==question_id).all() return render_template(\'detail.html\',ques=quest,comments=comments)# 把值quest丢进键quest,comments丢进comments在fabuview.html页面调用 @app.route(\'/comment/\',methods=[\'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\',question_id=ques_id))
读取前端页面数据,保存到数据库中
- 用<input type="hidden" 方法获取前端的"question_id"
- 显示评论次数
- 要求评论前登录
- 尝试实现详情页面下的评论列表显示
html文件:
{% extends \'myweb.html\' %} {% block detailtitle %}问答详情{% endblock %} {% block detailhead %} <link rel="stylesheet" type="text/css" href="../static/css/component.css"/> <script src="../static/js/regist.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> {% endblock %} {% block detailbody %} <div class="col-md-2 column "></div> <div class="col-md-8 column "> <ul class="list-unstyled"> <li> <h2 href="#" class="text-center">{{ ques.title }}</h2> <br> <p class="text-center"> <a href="#"> <small>{{ ques.author.username }}</small> </a>    <span class="pull-center"><small>{{ ques.create_time }}</small></span> </p> <p>{{ ques.detail }}</p> <form action="{{ url_for(\'comment\') }}" method="post"> <div class="form-group"> <textarea name="new_comment" class="form-control" rows="5" id="comment" placeholder="请输入评论"></textarea> <input type="hidden" name="question_id" value="{{ ques.id }}"> </div> <button type="submit" class="btn btn-default" style="margin-left:48% ">发送</button> </form> </li> </ul> <hr> <h4>评论:({{ ques.comments|length }})</h4> <ul class="list-unstyled"> {% for foo in comments %} <li class="list-group-item"> <a>{{ foo.author.username }}</a> <span class="badge pull-right">{{ foo.create_time }}</span> <p>{{ foo.detail }}</p> <br> </li> {% endfor %} </ul> </div> <div class="col-md-2 column "></div> {% endblock %}
运行截图:
以上是关于完成评论的主要内容,如果未能解决你的问题,请参考以下文章
如何设置 vscode 的代码片段,以便在自动完成后自动触发 vscode 的智能感知?