微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我正在尝试获取评论的ID以更新,删除并计算Upvotes和downvotes

如何解决我正在尝试获取评论的ID以更新,删除并计算Upvotes和downvotes

我试图获取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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?