栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【Flask项目】评论功能 # 15

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Flask项目】评论功能 # 15

评论展示:

 

评论功能仅对于登录用户有效,未登录的用户会显示登录发表评论。

而登录的用户可直接通过输入框进行评论。

评论相关: 后端Flask代码:
from . import blue_news
from flask import current_app, g, jsonify, request
from info.models import News, Comment
from info.utils.comment import check_login
from info import db
from info.response_code import RET

@blue_news.route('/news_comment', methods=["POST"])
@check_login
def news_comment():
    """
        评论相关
        1.校验用户登录状态
        2.接收参数(news_id, comment, parent_id)
        3.校验参数
            3.1 参数是否齐全(news_id, comment)
            3.2 news_id, parent_id是否为整型
            3.3 是否存在这条新闻
            3.4 是否存在这条父评论
        4.将评论添加进数据库
        5.返回结果
    :return:
    """
    # 1.校验用户登录状态
    user = g.user
    if not user:
        return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录")

    # 2.接收参数(news_id, comment, parent_id)
    news_id = request.json.get("news_id")
    comment = request.json.get("comment")
    parent_id = request.json.get("parent_id")

    # 3.校验参数
        # 3.1 参数是否齐全(news_id, comment)
    if not all([news_id, comment]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数缺失")

        # 3.2 news_id, parent_id是否为整型
    try:
        news_id = int(news_id)
        if parent_id:
            parent_id = int(parent_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

        # 3.3 是否存在这条新闻
        # 3.4 是否存在这条父评论
    f_comment = None
    try:
        news = News.query.filter_by(id=news_id).first()
        if parent_id:
            f_comment = Comment.query.filter_by(id=parent_id).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库查询失败")
    if not news:
        return jsonify(errno=RET.NODATA, errmsg="不存在的新闻")
    if parent_id and not f_comment:
        return jsonify(errno=RET.NODATA, errmsg="不存在的评论")

    # 4.将评论添加进数据库
    new_comment = Comment()
    new_comment.user_id = user.id
    new_comment.news_id = news.id
    if parent_id:
        new_comment.parent_id = parent_id
    new_comment.content = comment
    try:
        db.session.add(new_comment)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="评论失败")

    data = new_comment.to_dict()

    # 5.返回结果
    return jsonify(errno=RET.OK, errmsg="评论成功", data=data)
 Js代码:
    // 评论提交
    $(".comment_form").submit(function (e) {
        e.preventDefault();

        var news_id = $(this).attr('data-newsid')
        var news_comment = $(".comment_input").val();

        if (!news_comment) {
            alert('请输入评论内容');
            return
        }
        var params = {
            "news_id": news_id,
            "comment": news_comment
        };
        $.ajax({
            url: "/news/news_comment",
            type: "post",
            contentType: "application/json",
            headers: {
                "X-CSRFToken": getcookie("csrf_token")
            },
            data: JSON.stringify(params),
            success: function (resp) {
                if (resp.errno == '0') {
                    var comment = resp.data
                    // 拼接内容
                    var comment_html = ''
                    comment_html += ''
                    comment_html += ''
                    if (comment.user.avatar_url) {
                        comment_html += ''
                    }else {
                        comment_html += ''
                    }
                    comment_html += ''
                    comment_html += '' + comment.user.nick_name + ''
                    comment_html += ''
                    comment_html += comment.content
                    comment_html += ''
                    comment_html += '' + comment.create_time + ''

                    comment_html += '赞'
                    comment_html += '回复'
                    comment_html += '
' comment_html += '' comment_html += '' comment_html += '' comment_html += '
' comment_html += '' // 拼接到内容的前面 $(".comment_list_con").prepend(comment_html) // 让comment_sub 失去焦点 $('.comment_sub').blur(); // 清空输入框内容 $(".comment_input").val("") // 更新评论条数 updateCommentCount() }else { alert(resp.errmsg) } } }) });

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/348295.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号