问题描述
我试图获取ID来进行评论以进行更新,删除以及计算UpVotes和downVotes,直到我开始使用Django-mptt以获得可回复的评论之前,一切都工作正常。 现在,我试图通过node.id调用评论,但它不起作用。有没有人有任何建议或知道这是否可行(无需大量复杂代码)。这是我的代码 urls.py
path('<int:year>/<int:month>/<int:day>/<slug:post>/<node_id>/UpVote',views.UpVote_comment,name='UpVote_comment'),path('<int:year>/<int:month>/<int:day>/<slug:post>/<comment_id>/DownVote',views.DownVote_comment,name='DownVote_comment'),
views.py
def UpVote_comment(request,year,month,day,post,comment_id = None):
post = get_object_or_404(Post,slug=post,status='cleared',publish__year=year,publish__month=month,publish__day=day)
comments = post.comments.filter(active=True)
comment = Comment.objects.get(id=comment_id)
comment_form = CommentForm(data=request.POST)
user = request.user
if user in comment.UpVote.all():
comment.UpVote.remove(user)
else:
comment.UpVote.add(user)
if user in comment.DownVote.all():
comment.DownVote.remove(user)
comment.save(force_update=True)
comment_form = CommentForm()
messages.success(request,f'Comment has been UpVoted!')
return HttpResponseRedirect( post.get_absolute_url() )
def DownVote_comment(request,comment_id = None ):
post = get_object_or_404(Post,publish__day=day)
comments = post.comments.filter(active=True)
comment = Comment.objects.get(id=comment_id)
comment_form = CommentForm(data=request.POST)
user = request.user
if user in comment.DownVote.all():
comment.DownVote.remove(user)
else:
comment.DownVote.add(user)
if user in comment.UpVote.all():
comment.UpVote.remove(user)
comment.save(force_update=True)
comment_form = CommentForm()
messages.success(request,f'Comment has been DownVoted!')
return HttpResponseRedirect( post.get_absolute_url() )
models.py
class Comment(MPTTModel):
post = models.ForeignKey(Post,on_delete=models.CASCADE,related_name='comments')
parent = TreeForeignKey('self',on_delete=models.SET_NULL,null=True,blank=True,related_name='children')
name = models.ForeignKey(User,null=True)
body = models.TextField()
created = models.DateTimeField(auto_Now_add=True)
updated = models.DateTimeField(auto_Now=True)
active = models.BooleanField(default=True)
UpVote = models.ManyToManyField(settings.AUTH_USER_MODEL,related_name='comment_upVotes')
DownVote = models.ManyToManyField(settings.AUTH_USER_MODEL,related_name='comment_downVotes')
Flag = models.ManyToManyField(settings.AUTH_USER_MODEL,related_name='comment_flag')
class MPTTMeta:
order_insertion_by = ['-created']
def __str__(self):
return f'Comment by {self.name} on {self.post}'
detail.html
{% load mptt_tags %}
<div>
{% recursetree comments %}
<div class = "comments"
<div id= "{{ node.id }}" class="my-2 p-2" style="border: 1px solid grey">
<img class = "Cphoto"{% if node.name == NULL %} src = "/media/default.jpg " {% else %} src = '{{ node.name.profile.photo.url }}'{% endif %}>
<div class="d-flex justify-content-between"> By {{node.name}}<div>{{node.created }}</div>
</div>
<div> {{ node.body|linebreaks}} </div>
<div> {{node.id}}</div
</hr>
<ul class = "Cchange">
{% if user.is_authenticated %}
<li><a href = "{% url 'posts:UpVote_comment' post.publish.year post.publish.month post.publish.day post.slug comment.id %}" > Up Vote </a></li>
<li><button class="button" onclick="myFunction({{node.id}})">Reply</button></li>
{% endif %}
</div>
{% if not node.is_lead_node %}
<div class ="children pl-2 pl-md-5">
{{ children }}
</div>
{% endif %}
{% endrecursetree %}
</div>
</div>
我曾尝试像我之前说过的那样用node.id调用它,但是我无法使其正常工作 任何帮助或建议,将不胜感激,谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)