评论数效果如下:
image.png
image.png


如何获取评论数:

方法:使用filter筛选再用count方法统计计数

问题:页面和代码更加复杂了

先实现里面的样式

再blog/views.py中的blog_detail函数中填写

context['comment_count'] = Comment.objects.filter(content_type=blog_content_type,object_id=blog.pk).count()

然后再blog_detail.html中的阅读下面增加评论

<li>评论数:{{comment_count}}</li>

效果如下:
image.png
然后是实现列表中的评论数展示:

如果采用和阅读量一样的方法(先自己建一个方法,然后让视图处理函数继承该方法再调用)会比较麻烦,这里采用模板标签:
image.png
举例说明:
再comment中新建一个templatetags包,然后在templatetags中新建一个coment_tags.py文件,内部代码如下:

# emplatetags/comment_tags.py
from django import template

register = template.Library()

@register.simple_tag
def test():
    return 'this is a test code'

然后在blog_detail.html中先将comment_tag加载进来:

{% load comment_tag %}
...
<li>评论...
<li>{% test%}</li> 

效果如下:
image.png
具体操作:

重写comment_tags.py中的内容

from django import template
from django.contrib.contenttypes.models import ContentType
from ..models import Comment
from ..forms import CommentForm


register = template.Library()

@register.simple_tag
def get_comment_count(obj):
    content_type = ContentType.objects.get_for_model(obj)
    return Comment.objects.filter(content_type=content_type, object_id=obj.pk).count()

增加评论数在blog_list.html中的阅读量下面

{% load comment_tags %}
......
评论数:{% get_comment_count blog %}

然后效果如下:
image.png
使用自定义模板标签:
对应的是blog/views.py中的context['comment_form']
先在comment_tags.py中定义一个get_comment_form来获取评论表单(参数是对象)

from ..forms import CommentForm
...
@register.simple_tag
def get_comment_form(obj):
    content_type = ContentType.objects.get_for_model(obj)
    form = CommentForm(initial={
            'content_type': content_type.model,
            'object_id': obj.pk,
            'reply_comment_id': 0})
    return form

然后再blog_detail.html中回复下面的csrf下面加入如下代码:

{% get_comment_from blog as comment_form %}

然后为了避免两个comment_form干扰,去掉blog/views.py中的comment_form(context['comment_form']一行)
效果如下:
image.png
在comment\_tags.py中继续增加一个函数:具体获取的评论列表并排序返回

@register.simple_tag
def get_comment_list(obj):
    content_type = ContentType.objects.get_for_model(obj)
    comments = Comment.objects.filter(content_type=content_type, object_id=obj.pk, parent=None)
    return comments.order_by('-comment_time')

在前端使用上面写好的方法:blog_detail.html

<h3 class="comment-area-title">评论列表</h3>
<div id="comment_list">
{% get_comment_list blog as comments %}

然后去掉blog/views.py中的context['comments']内容,然后将comments和blog_content_type也可以去掉,没用了
image.png
效果如下:
image.png
小问题处理:
image.png
对于问题1,将comment/views.py中的strftime换成时间戳:timestamp(返回一段数字)
image.png
然后修改blog\_detail.html:

 function numFormat(num){
        return ('00' + num).substr(-2);
    }
 function timeFormat(timestamp){
            var datetime = new Date(timestamp * 1000);
            var year = datetime.getFullYear();
            var month = numFormat(datetime.getMonth() + 1);
            var day = numFormat(datetime.getDate());
            var hour = numFormat(datetime.getHours());
            var minute = numFormat(datetime.getMinutes());
            var second = numFormat(datetime.getSeconds());
            return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
        }

对于问题2:css样式调整
blog.css中增加dicv#reply_content_container

div#reply_content_container {
    border: 1px solid #d1d1d1;
    border-bottom: none;
    background-color: #f8f8f8;
    overflow: hidden;
    padding: 1em 1em 0.5em;
}
p#reply_title {
    border-bottom: 1px dashed #ccc;
    padding-bottom: 0.5em;
}

给对应的blog_detail.html标签增加class

    </div>
     <div id="reply_content_container" style="display:none;">
    <p id="reply_title">回复:</p>
    <div id="reply_content"></div>
</div>
  • 级联删除

对于on_delete=models.DO_NOTHING的说明:
image.png
就是说on\_delete = DO\_NOTHING时,例如对于一条评论如下:
image.png
如果删除这条评论,后面的回复依然会存在;但是如果on_delete=models.CASCADE时,会将后面的所有相关回复一起删除;

所以将文件中所有的DO_NOTHING替换成models.CASCADE,总共有九个
image.png

  • django-ckeditor:在增加comment模块之后,后台blogs中的博博文修改页面就会出错:

image.png
设置中没有default,此时在mysite/settings中的CKEDITOR_CONFIGS中添加default:
image.png
此时就可以正常修改了:
image.png


笨小孩
20 声望3 粉丝

你,要怎样度过这一生?有的人二十岁已经死了,有的人七十岁还在发现生命的可能,有人终其一生,不知道自己要的是什么;有人简单执拗,终其一生;忠于自我未必有结果,坚持努力也不一定换来成功,但有天,回望过...