简体   繁体   中英

Unable to display Post title at comment delete page

my community site has post objects and comments can be related to them. I have a extra comment delete template where i want to display the post title at the breadcrumbs of the page but for some reasone im unable to display the title as soon as i set the post object at the views.py context, any hint would be helpful. What im doing wrong here?

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)

It looks like you are using the same primary key for both the post and comment.

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

Are you sure this is correct? If you call this function with pk=1, it would get the comment and post with ID 1, maybe there is no post with that ID?

EDIT: If you have the Post referenced in your comment, access it like this:

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

As soon as you have deleted your commentinstance you cannot access the fields of the deleted instance, if you need to access to the 'post' pk related to the delet comment instance perhaps you could hadle 'post' instance before deleting the comment:

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)

Solution generated from the above answeres:

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)

many thanks for your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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