在编辑 post 的评论后,我无法重定向到 post

I can't redirect to the post after editing a comment of that post

编辑 post 的评论后,我无法重定向到 post。

models.py

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User


class Post(models.Model):
    body = models.TextField()
    created_on = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)


class Comment(models.Model):
    comment = models.TextField()
    created_on = models.DateTimeField(default=timezone.now)
    post = models.ForeignKey('Post', on_delete=models.CASCADE)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

views.py

class CommentEditView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
        model = Comment
        fields = ['comment']
        template_name = 'social/comment_edit.html'
        
        def get_success_url(self):
            pk = self.kwargs['pk']
            return reverse_lazy('post-detail',kwargs={'pk': pk,})
            
        def test_func(self):
            post = self.get_object()
            return self.request.user == post.author

comment_edit.html

{% extends 'landing/base.html' %}
{% load crispy_forms_tags %}

{% block content %}
<div class="container">
    <div class="row mt-5">
        <div class="col-md-3 col-sm-6">
            <a href="{% url 'post-detail' object.pk %}" class="btn btn-light">Back to Feed</a>
        </div>
    </div>
    <div class="row justify-content-center mt-5">
        <div class="col-md-5 col-sm-12">
            <h5>Update Your Comment</h5>
        </div>
    </div>
    <div class="row justify-content-center mt-3 mb-5">
        <div class="col-md-5 col-sm-12">
            <form method="POST">
                {% csrf_token %}
                {{ form | crispy }}
                <div class="d-grid gap-2">
                    <button class="btn btn-success mt-3">Update</button>
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock content %}

评论编辑正常。但是在提交编辑后的评论后,我想重定向到与评论相关的post。

pk评论的主键,不是post。您重定向:

class CommentEditView(LoginRequiredMixin, UpdateView):
    model = Comment
    fields = ['comment']
    template_name = 'social/comment_edit.html'

    def get_queryset(self, *args, **kwargs):
        return super().get_queryset(self, *args, **kwargs).filter(
            author=self.request.user
        )
        
    def get_success_url(self):
        return reverse_lazy('post-detail',kwargs={'pk': <strong>self.object.post_id</strong>})