• 评论功能的实现


    评论:

    • 根评论:对文章的评论

    • 子评论:对评论的评论

    • 区别:是否有父评论

    流程

    1. 构建样式

    2. 提交根评论

    3. 显示根评论

      1. -- render显示

      2. -- Ajax显示

    4. 提交子评论

    5. 显示子评论

      1. -- render显示

      2. -- Ajax显示

    html

        <!-- 评论 -->
        <div class="comments">
            <ul class="comment_list list-group">
                {% for comment in comment_list %}
                    <li class="list-group-item">
                        <div class="comment_info">
                            <a href="">#{{ forloop.counter }}楼 &nbsp;&nbsp;</a>
                            <span>{{ comment.created_time | date:'Y-m-d H:i' }} </span> &nbsp;&nbsp;
                            <a href=""><span>{{ comment.user.username }}</span></a>
                            <a class="pull-right reply_btn" username="{{ comment.user.username }}"
                               comment_pk='{{ comment.pk }}'>回复</a>
                        </div>
    
                        {% if comment.parent_comment_id %}
                            <div class="pid_info well">  <!-- well:和子评论形成间隙 -->
                                <p>
                                    {{ comment.parent_comment.user.username }}: {{ comment.parent_comment.content }}
                                </p>
                            </div>
                        {% endif %}
                        <div class="show_comment_content">
                            <p>{{ comment.content }}</p>
                        </div>
    
                    </li>
                {% endfor %}
            </ul>
            <p>评论列表</p>
            <p>发表评论</p>
            <p>
                昵称:<input type="text" id="tbCommentAuthor" class="author" disabled="disabled" size="50"
                          value="{{ request.user.username }}">
            </p>
            <p>评论内容:</p>
            <p>
                <textarea name="" id="comment_content" cols="60" rows="10"></textarea>
            </p>
    
            <button class="btn btn-default" id="comment_btn">提交评论</button>
        </div>

    css

    /* 评论 */
    input.author {
        background-image: url(/static/font/icon_form.gif);
        background-repeat: no-repeat;
        background-position: 3px -3px;
        border: 1px solid #ccc;
        padding: 4px 4px 4px 30px;
        width: 300px;
        font-size: 13px;
    }
    
    .show_comment_content {
        margin-top: 10px;
    }
    
    .comment_info a {
        cursor: pointer;
        text-decoration: none;
    }
    
    .comment_info a:hover {
        color: #5cb85c;
    }

    js

            // 点赞请求
            $('#div_digg .action').click(function () {
                let is_up = $(this).hasClass('diggit');
    
                $obj = $(this).children('span');
    
                $.ajax({
                    url: '/digg/',
                    type: 'post',
                    data: {
                        'csrfmiddlewaretoken': $("[name='csrfmiddlewaretoken']").val(),
                        'is_up': is_up,
                        'article_id': "{{ article_obj.pk }}",
                    },
                    success: function (data) {
                        if (data.status) {
                            let val = parseInt($obj.text());
                            $obj.text(val + 1);
                        } else {
                            let val = data.handled ? '您已经推荐过!' : '您已经反对过!';
                            $('#digg_tips').html(val);
    
                            setTimeout(function () {
                                $('#digg_tips').html("");
                            }, 1000)
                        }
                    }
                })
            });
    
            // 评论请求
            let pid = '';
            $('#comment_btn').click(function () {
                let content = $('#comment_content').val();
    
                if (pid) {
                    let index = content.indexOf("
    ");
                    content = content.slice(index + 1);
    
                }
                $.ajax({
                    url: '/comment/',
                    type: 'post',
                    data: {
                        'csrfmiddlewaretoken': $("[name='csrfmiddlewaretoken']").val(),
                        'article_id': "{{ article_obj.pk }}",
                        'content': content,
                        'pid': pid,
                    },
                    success: function (data) {
                        let created_time = data.created_time;
                        let username = data.username;
                        let content = data.content;
    
                        if (data.parent_comment) {  // 用户提交的是子评论,同时显示父评论
                            let latest_comment = `
                        <li class="list-group-item">
                        <div class='well'>
                            <p>${data.parent_name}: ${data.parent_comment}</p>
                        </div>
                        <div>
                            <span>${created_time}</span> &nbsp;&nbsp;
                            <a href=""><span>${username}</span></a>
                        </div>
                        <div class="show_comment_content">
                            <p>${content}</p>
                        </div>
                        </li>`;
                            $('ul.comment_list').append(latest_comment);
    
                        } else {  // 用户提价的是根评论,只显示用户提交的评论
                            let latest_comment = `
                        <li class="list-group-item">
                        <div>
                            <span>${created_time}</span> &nbsp;&nbsp;
                            <a href=""><span>${username}</span></a>
                        </div>
                        <div class="show_comment_content">
                            <p>${content}</p>
                        </div>
                        </li>`;
                            $('ul.comment_list').append(latest_comment);
                        }
    
                        // 清空评论框
                        $('#comment_content').val('');
                        pid = "";
                    }
                })
            });
    
            // 回复按钮事件
            $('.reply_btn').click(function () {
                $('#comment_content').focus();
                let comment_user = '@' + $(this).attr('username') + "
    ";
                $('#comment_content').val(comment_user);
    
                pid = $(this).attr('comment_pk');
            });

    py

    # urls.py
    
    # 评论
    path('comment/', views.comment),
    
    views.py
    
    # 评论
    def comment(request):
        article_id = request.POST.get("article_id")
        pid = request.POST.get('pid')
        content = request.POST.get('content')
        user_id = request.user.pk
    
        comment_obj = models.Comment.objects.create(
            user_id=user_id,
            article_id=article_id,
            content=content,
            parent_comment_id=pid
        )
    
        response = {}
        response['created_time'] = comment_obj.created_time.strftime('%Y-%m%d %X')
        response['username'] = request.user.username
        response['content'] = content
        if pid:
            parent_comment = models.Comment.objects.filter(nid=pid).first()
            response['parent_comment'] = parent_comment.content
            response['parent_name'] = parent_comment.user.username
    
        return JsonResponse(response

  • 相关阅读:
    模型评估方法
    欠拟合、过拟合、偏差、方差
    机器学习基本概念
    Hive 的基本概念
    Flume的Channel
    Flume的Sink
    Flume的Source
    Flume 安装和配置
    Flume的基本概念
    BIO & NIO & NIO常见框架
  • 原文地址:https://www.cnblogs.com/lshedward/p/10396594.html
Copyright © 2020-2023  润新知