Как мне включить get_absolute_url в мой success_url представления класса

как включить get_absolute_url, определенный в модели, в представление на основе класса?

модель

class Comment(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name="comments")
    name = models.ForeignKey(User, on_delete=models.CASCADE)
    body = models.TextField(default="This is the Body of a Comment.")
    date_added = models.DateField(auto_now_add=True)
    time_added = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateField(auto_now=True)
    time_updated = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name_plural = "Post Comments"
        ordering = ["-time_updated"]

    def __str__(self):
        return self.post.title + " | " + self.name.username

    def get_absolute_url(self):
        return f"/blogs/post/{self.post.slug}"

view

class DeleteCommentView(DeleteView):
    model = Comment
    template_name = "delete_comment.html"
    success_url = (refer the get_absolute_url)

Вы можете переопределить метод .get_success_url() [Django-doc]:

class DeleteCommentView(DeleteView):
    model = Comment
    template_name = 'delete_comment.html'
    
    def get_success_url(self):
        return self.object.get_absolute_url()

Но я думаю, что здесь вы используете метод .get_absolute_url() неправильно: см. примечание ниже.


Note: The get_absolute_url() method [Django-doc] should return a canonical URL, that means that for two different model objects the URL should be different and thus point to a view specific for that model object. You thus should not return the same URL for all model objects.

Вернуться на верх