diff --git a/comments_tags.py b/comments_tags.py new file mode 100644 index 0000000..0304c6c --- /dev/null +++ b/comments_tags.py @@ -0,0 +1,37 @@ +from django import template + +#zr 注册模板标签库 +register = template.Library() + +#zr 解析评论树的模板标签 +@register.simple_tag +def parse_commenttree(commentlist, comment): + """获得当前评论子评论的列表 + 用法: {% parse_commenttree article_comments comment as childcomments %} + """ + datas = [] + + #zr 递归解析子评论的内部函数 + def parse(c): + #zr 获取当前评论的直接子评论 + childs = commentlist.filter(parent_comment=c, is_enable=True) + for child in childs: + #zr 将子评论添加到结果列表 + datas.append(child) + #zr 递归解析子评论的子评论 + parse(child) + + #zr 从传入的评论开始解析 + parse(comment) + return datas + +#zr 显示评论项的包含标签 +@register.inclusion_tag('comments/tags/comment_item.html') +def show_comment_item(comment, ischild): + """评论""" + #zr 根据是否为子评论设置不同的深度 + depth = 1 if ischild else 2 + return { + 'comment_item': comment, + 'depth': depth + }