Flask - 评论

页面编写

 <div id="comment">
    <p>文章评论:</p>
    <div>
        <form action="{{ url_for('article.article_comment') }}" method="post">
            <input type="hidden" name="aid" value="{{ article.id }}">
            <p>
        <textarea name="comment" class="form-control" style="width: 60%" rows="5" cols="60">写下你想说的,开始我们的对话
        </textarea>
            </p>
            <p><input type="submit" value="评论" class="btn btn-info"></p>
        </form>
    </div>
    <p style="margin: 20px 5px; border-bottom: 1px solid lightgray">

    </p>
    <div id="comment_item">
        {% if comments.items %}
            {% for comment in comments.items %}
                <div class="item">
                    <div id="item_left">
                        <img src=" {% if comment.user.icon %} {{ url_for('static',filename=comment.user.icon) }} {% else %}{{ url_for('static',filename='images/touxiang.jpg') }}{% endif %}"
                             alt="">
                    </div>
                    <div id="item_right">
                        <p>
                            <span>{{ comment.user.username }}</span>
                            <span>{{ comment.cdatetime }}</span>
                        </p>
                        <p>
                            {{ comment.comment }}
                        </p>
                    </div>
                </div>
            {% endfor %}
        {% else %}
            <div class="item">
                当前文章还没有任何评论,赶快发表意见吧~~~~
            </div>
        {% endif %}
    </div>
    <div id="paginate">

        <nav aria-label="...">
            <ul class="pager">
                <li class="previous {% if not comments.has_prev %}disabled{% endif %}"><a
                        href="{{ url_for('article.article_detail') }}?page={{ comments.prev_num }}&aid={{ article.id }}"><span
                        aria-hidden="true">&larr;</span> 上一页</a></li>

                <li class="next {% if not comments.has_next %}disabled {% endif %}"><a
                        href="{{ url_for('article.article_detail') }}?page={{ comments.next_num }}&aid={{ article.id }}">下一页
                    <span
                            aria-hidden="true">&rarr;</span></a></li>
            </ul>
        </nav>

    </div>
</div>

视图

# 评论
@article_bp.route('/article_comment', methods=['GET', 'POST'])
def article_comment():
    article_id = request.form.get('aid')
    comment = Comment()
    comment_content = request.form.get('comment')
    comment.article_id = article_id
    comment.comment = comment_content
    comment.user_id = g.user.id
    db.session.add(comment)
    db.session.commit()
    return redirect(url_for('article.article_detail')+"?aid="+article_id)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值