添加子评论

comment_list = []
for comment in comments:
for item in comment_list:
if not hasattr(item, 'children_comment'):
setattr(item, 'children_comment', [])
if comment.pid == item:
item.children_comment.append(comment)
break
if comment.pid is None:
comment_list.append(comment)
查看是不是有子评论,有的话就加到子评论列表里面
        if not hasattr(item, 'children_comment'):
setattr(item, 'children_comment', [])
判断item里面有没有children_comment的属性,没有的话,就给他加一个children_comment的属性,并初始化为[]
        if comment.pid == item:
item.children_comment.append(comment)
判断当前对象是不是父评论对象
comment_form = CommentForm({'author': request.user.username,
'email': request.user.email,
'url': request.user.url,
'article': id} if request.user.is_authenticated() else{'article': id})
初始化Comment

Form实例对象,如果user存在,因为使用了Django的表单验证,所以Django会把user对象传递到request当中,(我是这么理解的不知道对不对),然后就实例化了一个CommnetForm对象

 request.user.is_authenticated
comment_form = CommentForm(request.POST)





posted @ 2016-08-08 15:41  意发并行  阅读(191)  评论(0编辑  收藏  举报