小白个人博客---文章评论实现
1.修改以前的数据:
增加parent_id,评论的第一楼值为none,没有父级
子评论的parent_id即为上级评论的nid
reply即回复的内容,因为他是对已经存在的Comment进行回复,同时他本身也是一条评论,可以被其他人继续回复,所以是自关联。
class Comment(models.Model): """用户评论""" nid=models.BigAutoField(primary_key=True) parent_id=models.IntegerField(verbose_name="父级评论id",null=True,blank=True) reply=models.ForeignKey(verbose_name="回复评论",to="self",to_field="nid",related_name="rep",blank=True,null=True,on_delete=models.CASCADE) content=models.CharField(verbose_name="评论内容",max_length=255) create_time=models.DateTimeField(verbose_name="创建时间",auto_now_add=True) article=models.ForeignKey(verbose_name="评论文章",to="Article",to_field="nid",on_delete=models.CASCADE) user=models.ForeignKey(verbose_name="评论者",to="UserInfo",to_field="uid",on_delete=models.CASCADE)
2.views:
1.回顾文章详细detail:
def detail(requset, site, nid): blog = models.Blog.objects.filter(site=site).select_related("user").first() tag_list = models.Tag.objects.filter(blog=blog.bid) category_list = models.Category.objects.filter(blog=blog) date_list = models.Article.objects.raw( 'select nid,count(nid) as num,strftime("%Y-%m",create_time) as ctime from repository_article where blog_id=' + str(blog.bid) + ' group by strftime("%Y-%m",create_time)' ) article = models.Article.objects.filter(blog=blog, nid=nid).select_related("category").first() comment_list = models.Comment.objects.filter(article=article).values("nid","parent_id","user__username","content","create_time") comment_str = comment_data_handle(comment_list) #交给其他函数处理 print("requ", requset.user) return render(requset, "home_detail.html", { "blog": blog, "article": article, "tag_list": tag_list, "category_list": category_list, "date_list": date_list, "comment_str": comment_str, #让后台生成html,直接传到模板 })
def comment_data_handle(comment_list): #所有的字评论按父级分组 print('comm_list',comment_list) comment_dict={} for item in comment_list: item["child"]=[] comment_dict[item["nid"]]=item #重新处理comment_list:{1:{nid:1,...child:[]}} print("comment_dict",comment_dict) result=[] for v in comment_dict.values(): if not v['parent_id']: #拿出第一楼的,没有parent_id result.append(v) else: comment_dict[v['parent_id']]['child'].append(v) #其他的根据各自的parent_id,添加到parent的child res=comment_tree(result) #到此数据的格式处理完毕,有parent_id的都加到child里面 return res
def comment_tree(result_list): #生成第一楼的html response="" text=""" <div style="border:1px solid"> <span>评论人:%s 评论时间:%s</span> <a cid=%s class="reply">回复</a> <div>评论内容:%s</div> <div style="margin-left:20px;">%s</div> </div> <hr/> """ for item in result_list: user=item["user__username"] ctime=item["create_time"].strftime("%Y-%m-%d %H:%M:%S") content=item["content"] child_content=child_content_hanndle(item["child"]) #处理子评论 response+= text %(user,ctime,item['nid'],content,child_content) print(response) return response
def child_content_hanndle(child_list): response = "" text = """ <div style="border:1px solid"> <span>评论人:%s 评论时间:%s</span> <a cid=%s class="reply">回复</a> <div>评论内容:%s</div> <div style="margin-left:20px;">%s</div> </div> <hr/> """ for item in child_list: user = item["user__username"] ctime = item["create_time"].strftime("%Y-%m-%d %H:%M:%S") content = item["content"] child_child_content=child_content_hanndle(item["child"]) #递归处理child的child response += text % (user, ctime,item['nid'], content,child_child_content) return response



浙公网安备 33010602011771号