zoukankan      html  css  js  c++  java
  • Django博客功能实现—文章评论的显示

    功能:
    在打开文章之后,能在文章下面是显示文章的评论,有父级评论。
    思路:
    在文章详情的视图里面,获取这个文章的全部评论,得到显示列表,然后用模板显示出来。
    步骤:
    一,在views.py的文章详情中获取评论:

     1 #文章详情
     2 #blog/views.py
     3 def article(request):
     4     try: 
     5         # 获取评论信息
     6         #查询指定文章的所有评论,按照id排序
     7         comments = Comment.objects.filter(article=article).order_by('id')
     8         #创建一个评论的空列表
     9         comment_list = []
    10         #遍历一篇文章中的所有评论
    11         for comment in comments:
    12             for item in comment_list:
    13                 #判断item中是否有"children_comment"属性,返回布尔值
    14                 if not hasattr(item, 'children_comment'):
    15                     #若无"children_comment"属性,则设置这个属性,属性的值为[]
    16                     setattr(item, 'children_comment', [])
    17                 #如果item和父级评论的值相等,则讲该遍历出来的文章添加到"children_comment"属性,然后跳出当前循环
    18                 if comment.pid == item:
    19                     item.children_comment.append(comment)
    20                     break
    21             #如果父级评论为空,则讲给评论放到父级评论列表里面
    22             if comment.pid is None:
    23                 comment_list.append(comment)
    24     except Exception as e:
    25         print e
    26         logger.error(e)
    27     return render(request, 'article.html', locals())

    二、在模板中显示出来

     1 #article.html
     2           {% for comment in comment_list %}
     3        
     4         <a href='{{ comment.url }}'>{{ comment.username }}</a><a href="#comment-59418">{{ comment.date_publish | date:'Y-m-d H:i:s' }}</a>
     5                 <div><img alt='' src='{% static 'images/default.jpg' %}' class='avatar avatar-32 photo' height='32' width='32' /></div>
     6 
     7                                 <p>{{ comment.content }}</p>
     8                 </div>
     9           </li>
    10           {% for children_comment in comment.children_comment %}
    11           <li id="comment-59542">
    12                 <div class="reply">
    13                   <div class="top"><a href="{{ children_comment.url }}" rel="external nofollow" class="url">{{ children_comment.username }}</a><span class="time"> @ <a href="#comment-59543" title="">{{ children_comment.date_publish | date:'Y-m-d H:i:s' }}</a></span></div>
    14                   <div><img alt="" src="{% static 'images/default.jpg' %}" class="avatar avatar-32 photo" height="32" width="32"></div>
    15 
    16                   <div class="body">
    17                                         {{ children_comment.content }}                  </div>
    18                 </div>
    19           </li>
    20           {% endfor %}

    如此就能讲一个文章的评论显示出来

    作者:岑宇
    出处:http://www.cnblogs.com/cenyu/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    如果文中有什么错误,欢迎指出。以免更多的人被误导。
    >
  • 相关阅读:
    SAP常用Tcode汇总
    SAP物料管理标准报表
    Linux系统将大量的图片合成.gif
    fluent对网格进行透明显示
    fluent计算结果进行镜像显示
    Fluent显示中间截面附近的颗粒
    fluent提取壁面上一条线上的冲蚀磨损量
    cfdemSolverPisoScalar和cfdemSolverPisoSTM的区别
    paraview处理fluent 2020R2计算结果
    关于fluent中的压力(二)和(三)
  • 原文地址:https://www.cnblogs.com/cenyu/p/5713387.html
Copyright © 2011-2022 走看看