码迷,mamicode.com
首页 > 其他好文 > 详细

完成评论功能

时间:2017-12-08 23:00:55      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:app   submit   extend   bootstra   extends   val   max   lis   detail   

        定义评论的视图函数
        @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>&nbsp&nbsp&nbsp
                    <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 %}

 

完成评论功能

标签:app   submit   extend   bootstra   extends   val   max   lis   detail   

原文地址:http://www.cnblogs.com/0542054ghgf/p/8007097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!