栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

django实现多级评论功能

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

django实现多级评论功能

class Comment(MPTTModel): public models.ForeignKey(PublishInformation, on_delete models.CASCADE, related_name comments_detail , verbose_name 关联的内容 ) user models.ForeignKey(User, on_delete models.CASCADE, related_name comments_user , verbose_name 评论的人 ) userNickname models.CharField(max_length 50, verbose_name 评论人的昵称 , default 匿名 ) body models.TextField(verbose_name 评论内容 ) created models.DateTimeField(auto_now_add True) parent TreeForeignKey( self , on_delete models.CASCADE, null True, blank True, related_name children ) # 新增 记录二级评论回复给谁, str reply_to models.ForeignKey(User, null True, blank True, on_delete models.CASCADE, related_name replyers ) # class meta: # ordering ( created ,) class MPTTmeta: order_insertion_by [ created ] ordering [ -created ] class meta: ordering [ -created ] def __str__(self): return self.body[:20]

4、view视图

# 文章评论
#增加评论
def addComment(request):
 print(request.POST)
 user request.user
 userNickname request.POST.get( userNickname )
 body request.POST.get( body )
 parent_id request.POST.get( parent )
 public get_object_or_404(PublishInformation, id request.POST.get( public ))
 if parent_id:
 parent get_object_or_404(Comment, id parent_id)
 reply_to parent.user
 comment Comment(public public, user user, userNickname userNickname, body body, parent parent, reply_to reply_to)
 else:
 comment Comment(public public, user user, userNickname userNickname, body body)
 comment.save()
 return redirect( detail , public.id)

文章详情页的视图

def detail(request, id):
 publicInfor PublishInformation.objects.get(id id)
 comments Comment.objects.filter(public publicInfor)
 return render(request, AreaTemplates/detail.html , { publicInfor : publicInfor, comments :comments})

5、html页面

{% recursetree comments %}
 div 
 div class flex items-center mt-2 img class h-6 rounded-full 
 src https://images.unsplash.com/photo-1583512603805-3cc6b41f3edb?ixlib rb-1.2.1 amp;ixid MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8 amp;auto format amp;fit crop amp;w 800 amp;q 80 
 alt span class ml-2 text-sm {{ node.userNickname }} /span /div 
 div class text-sm ml-8 {{ node.body |safe}} /div 
 a class ml-8 hover:text-blue-700 cursor-pointer style font-size: xx-small aria-hidden true data-toggle modal data-target #_{{node.pk}} 
 /div 
 {% if not node.is_leaf_node %}
 div class ml-8 
 {{ children }}
 /div 
 {% endif %}
 form method post action {% url addComment %} style position:absolute 
 {% csrf_token %}
 div class modal fade id _{{node.pk}} tabindex -1 role dialog aria-labelledby ModalCenterTitle aria-hidden true 
 div class modal-dialog modal-dialog-centered role document 
 div class modal-content 
 div class modal-header 
 h5 class modal-title id exampleModalCenterTitle 回复 {{node.userNickname}} /h5 
 button type button class close data-dismiss modal aria-label Close 
 span aria-hidden true times; /span 
 /button 
 /div 
 div class modal-body 
 input type hidden name parent value {{ node.pk }} 
 input type hidden name public value {{ publicInfor.id }} 
 input type text class form-control placeholder 昵称 name userNickname 
 textarea type text class form-control mt-2 placeholder 请输入... name body /textarea 
 /div 
 div class modal-footer 
 button type button class btn btn-secondary data-dismiss modal 关闭 /button 
 button type submit class btn btn-primary 新建 /button 
 /div 
 /div 
 /div 
 /div 
 /form 
{% endrecursetree %}

参考
文档
杜赛的博客

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/266800.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号