如何获取评论数:
方法:使用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中的阅读下面增加评论
评论数:{{comment_count}}
如果采用和阅读量一样的方法(先自己建一个方法,然后让视图处理函数继承该方法再调用)会比较麻烦,这里采用模板标签:
举例说明:
再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 %}
...
评论...
{% test%}
重写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 %}
然后效果如下:
使用自定义模板标签:
对应的是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']一行)
效果如下:
在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
评论列表
{% get_comment_list blog as comments %}
然后去掉blog/views.py中的context['comments']内容,然后将comments和blog_content_type也可以去掉,没用了

然后修改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
- 级联删除
对于on_delete=models.DO_NOTHING的说明:
如果删除这条评论,后面的回复依然会存在;但是如果on_delete=models.CASCADE时,会将后面的所有相关回复一起删除;
所以将文件中所有的DO_NOTHING替换成models.CASCADE,总共有九个
- django-ckeditor:在增加comment模块之后,后台blogs中的博博文修改页面就会出错: