完成评论功能

    1. 定义评论的视图函数
      @app.route('/comment/',methods=['POST'])
      def comment():
      读取前端页面数据,保存到数据库中
    2. 用<input type="hidden" 方法获取前端的"question_id" 
    3. 显示评论次数
    4. 要求评论前登录
    5. 尝试实现详情页面下的评论列表显示

主py文件:

def loginFirst(func):
    #行动前需要登录,定义装饰器
    @wraps(func)
    def wrapper(*args,**kwargs):
        if session.get('user'):
            return func(*args,**kwargs)
        else:
            return redirect(url_for('dl'))
    return wrapper

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

详情页HTML:

{% extends 'dh.html' %}
{% block title %}
    反馈详情
{% endblock  %}
{% block head %}
        <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='../static/css/xq.css') }}">
{% endblock  %}
{% block main %}
   <div class="page-header">
   <h3>问题:{{ ques.title }}<br>
       <small>作者:{{ ques.author.username }}<br>
           <span class="">
               发布时间:{{ ques.creat_time }}
           </span></small></h3>
    <p class="lead">详情:{{ ques.detail }} </p>
       <form role="form" action="{{ url_for('comment') }}" method="post">
    <div class="form-group">
        <textarea  name="new_comment" class="form-control" rows="6" id="questionTitle" placeholder="请写下你的评论" style="height: 100px" ></textarea>
        <input name="question_id" type="hidden" value="{{ ques.id }}">
    </div>
   <div class="submit-button">
       <br>
       <button type="submit" style="float:right" id="submit-button">发送</button>
    </div></form></div>
    <div class="pl-box">
       <h4>评论区:
       ({{ ques.comments|length }})</h4><br>

    <ul class="list-group">
            {% for ques in ques.comments %}
            <li class="list-group-item">
            <img style="width: 30px" src="{{ url_for('static',filename='../static/image/tx.jpg') }}" alt="64">
                <a href="#">{{ ques.author.username }}</a><br>
                <p>评论详情:{{ ques.detail }}</p><br>
                <span class="badge" style="margin-left: 60%">{{ ques.creat_time }}评论时间</span>
                <p style="margin-left: 25%"></p>
            </li>
            {% endfor %}
        </ul>
    </div>


{% endblock %}

 

 

posted @ 2017-12-08 11:38  101黎思琦  阅读(108)  评论(0编辑  收藏  举报