繁体   English   中英

无法在评论删除页面上显示帖子标题

[英]Unable to display Post title at comment delete page

我的社区站点上有张贴对象,评论可以与它们相关。 我有一个额外的评论删除模板,我想在该页面的面包屑上显示帖子标题,但是由于某种原因,我将帖子对象设置在views.py上下文中后,我无法立即显示标题,任何提示都是很有帮助。 我在这里做错了什么?

views.py

def comment_delete(request, pk):
    comment = get_object_or_404(Comment, pk=pk)
    post = get_object_or_404(Post, pk=pk)
    if request.user == comment.author:
        if request.method == 'POST':
            comment.delete()
            messages.success(request, 'You have successfully deleted the comment.')
            return redirect('post_detail', pk=comment.post.pk)
        else:
            template = 'app/Post/post_comment_delete.html'
            form = CommentForm(instance=comment)
            context = {
                'comment': comment,
                'form': form,
                'post': post
            }
            return render(request, template, context)
    else:
        messages.warning(request, 'Comment could not be deleted.')
        return redirect('post_detail', pk=comment.post.pk)

template.html:

<a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }} </a>

models.py

class Comment(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    post = models.ForeignKey(Post, on_delete=models.CASCADE)
    content = models.TextField(max_length=500)
    published_date = models.DateField(auto_now_add=True, null=True)

您似乎对帖子和评论使用了相同的主键。

def comment_delete(request, pk):
    comment = get_object_or_404(Comment, pk=pk)
    post = get_object_or_404(Post, pk=pk)

您确定这是正确的吗? 如果您以pk = 1调用此函数,它将获得注释并发布ID为1的帖子,也许没有发布该ID的帖子?

编辑:如果您在评论中引用了帖子,请像这样访问它:

def comment_delete(request, pk):
    comment = get_object_or_404(Comment, pk=pk)
    post = comment.post

删除注释实例后,您将无法访问已删除实例的字段,如果您需要访问与删除注释实例相关的'post'pk,也许您可​​以在删除注释之前先删除'post'实例:

if request.method == 'POST':
    related_post = Post.objects.get(pk=comment.post.pk)
    comment.delete()
    messages.success(request, 'You have successfully deleted the comment.')
    return redirect('post_detail', pk=related_post.pk)

根据以上答案生成的解决方案:

def comment_delete(request, pk):
    comment = get_object_or_404(Comment, pk=pk)
    post = comment.post
    if request.user == comment.author:
        if request.method == 'POST':
            post = Post.objects.get(pk=comment.post.pk)
            comment.delete()
            messages.success(request, 'You have successfully deleted the comment.')
            return redirect('post_detail', pk=post.pk)
        else:
            template = 'app/Post/post_comment_delete.html'
            form = CommentForm(instance=comment)
            context = {
                'comment': comment,
                'post': post,
                'form': form

            }
            return render(request, template, context)
    else:
        messages.warning(request, 'Comment could not be deleted.')
        return redirect('post_detail', pk=comment.post.pk)

非常感谢您的帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM