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 %}
参考
文档
杜赛的博客



