BBS-文章详情页、评论、评论树BBS-文章详情页、评论、评论树

 1、简单的实现评论功能

article_detail.html,拿到数据  由路--给视图函数--写入数据库

  <p>评论内容:</p>
        <textarea name="" id="comment_content" cols="60" rows="10"></textarea>
        <p>
            <button class="btn btn-default comment_btn">提交评论</button>
        </p>
url
path("comment/", views.comment),

--

 <script>
        $('.comment_btn').click(function () {
            {#根评论#}
            var pid='';
            var content=$('#comment_content').val();
            $.ajax({
                url:'/comment/',
                type:'post',
                data:{  "csrfmiddlewaretoken": $("[name='csrfmiddlewaretoken']").val(),
                'article_id':'{{ article_obj.pk }}','content':content ,'pid':pid},
                success:function (data) {
                    console.log(data)
                }

            })
        })

 ---

def comment(request):
    """
    提交评论视图函数
    功能:
    1 保存评论
    2 创建事务
    3 发送邮件
    :param request:
    :return:
    """
    print(request.POST)

    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)

 BBS-文章详情页、评论、评论树

BBS-文章详情页、评论、评论树

评论之后--清空评论框

 BBS-文章详情页、评论、评论树

 显示根评论的两种方法

1、render显示根评论

BBS-文章详情页、评论、评论树

 

 ---

class="list-group

BBS-文章详情页、评论、评论树

article_detail.html

 <p>评论列表</p>
        <ul class="list-group comment_list">
            {% for comment in comment_list %}
                <li class="list-group-item">
                    <div>
                        <a href=""># {{ forloop.counter }}楼</a> &nbsp;&nbsp;
                        <span>{{ comment.create_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><div class="comment_con">
                        <p>{{ comment.content }}</p>
                    </div>
                </li>
            {% endfor %}
        </ul>

BBS-文章详情页、评论、评论树BBS-文章详情页、评论、评论树

 2、ajax显示评论:es6 模板字符串

上一种方法评论后刷新才显示---用ajax评论就显示

 BBS-文章详情页、评论、评论树

 

success:function (data) {
                    console.log(data);

                    var create_time = data.create_time;
                    var username=data.username;
                    var content=data.content;

                    // 构建标签字符串 es6语法
                     var s = `
                           <li class="list-group-item">
                              <div>

                                  <span>${create_time}</span>&nbsp;&nbsp;
                                  <a href=""><span>${username}</span></a>

                              </div>
                              <div class="comment_con">
                                  <p>${content}</p>
                              </div>

                            </li>`;

                        $("ul.comment_list").append(s);

def comment():

 response={}
    response['create_time']=comment_obj.create_time.strftime("%Y-%m-%d %X")
    response['username']=request.user.username
    response['content']=content
  return JsonResponse(response)

 根评论

{#    评论#}
    <div class="comments list-group">
        <p class="tree_btn">评论树</p>
        <div class="comment_tree">
        </div>
        <p>评论列表</p>
        <ul class="list-group comment_list">
            {% for comment in comment_list %}
                <li class="list-group-item">
                    <div>
                        <a href=""># {{ forloop.counter }}楼</a> &nbsp;&nbsp;
                        <span>{{ comment.create_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">
                            <p>
                                {{ comment.parent_comment.user.username }}: {{ comment.parent_comment.content }}
                            </p>
                        </div>
                    {% endif %}
                    <div class="comment_con">
                        <p>{{ comment.content }}</p>
                    </div>
                </li>
            {% endfor %}
        </ul>
        <p>发表评论</p>
        <p>昵称:<input type="text" id="tbCommentAuthor" class="author" disabled="disabled" size="50"
                     value="{{ request.user.username }}">
        </p>
        <p>评论内容:</p>
        <textarea name="" id="comment_content" cols="60" rows="10"></textarea>
        <p>
            <button class="btn btn-default comment_btn">提交评论</button>
        </p>
    </div>

    <script>
        $('.comment_btn').click(function () {
            {#根评论#}
            var pid='';
            var content=$('#comment_content').val();
            $.ajax({
                url:'/comment/',
                type:'post',
                data:{  "csrfmiddlewaretoken": $("[name='csrfmiddlewaretoken']").val(),
                'article_id':'{{ article_obj.pk }}','content':content ,'pid':pid},
                success:function (data) {
                    console.log(data);

                    var create_time = data.create_time;
                    var username=data.username;
                    var content=data.content;

                    // 构建标签字符串 es6语法
                     var s = `
                           <li class="list-group-item">
                              <div>

                                  <span>${create_time}</span>&nbsp;&nbsp;
                                  <a href=""><span>${username}</span></a>

                              </div>
                              <div class="comment_con">
                                  <p>${content}</p>
                              </div>

                            </li>`;

                        $("ul.comment_list").append(s);



                    // 传回数据后,清空评论框中的内容
                    $('#comment_content').val('');
                }

            })
        })
    </script>
View Code

相关文章:

  • 2022-12-23
  • 2021-12-16
  • 2022-12-23
  • 2021-12-13
  • 2021-10-01
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-16
  • 2022-03-09
  • 2021-07-24
  • 2021-07-05
  • 2021-10-25
相关资源
相似解决方案