我试图删除我的Django项目中一个动态成功URL的评论,但它给了我一个404

j7dteeu8  于 2023-10-21  发布在  Go
关注(0)|答案(1)|浏览(99)

所以这个视图的代码是我的模型,URL和一切都很好,我去检查我手动请求的数据,有趣的是它出现在那里。有人能告诉我这是什么错误吗?

class CommentDeleteView(OwnerDeleteView):
    model = Comment
    template_name = "ads/comment_delete.html"

# https://stackoverflow.com/questions/26290415/deleteview-with-a-dynamic-success-url-dependent-on-id
    def get_success_url(self):
        ad = self.object.ad
        ctx={'ad':ad.id}
        return reverse_lazy('ads:ad_detail', ctx)

ngynwnxp

ngynwnxp1#

你可以简单地使用Django中的通用视图来删除注解。

from django.views.generic import DeleteView

然后简单地使用如下代码

class CommentDeleteView(DeleteView):
    model = Comment
    template_name = "ads/comment_delete.html"

在此之后,您可以编辑您的urls.py文件,

from django.urls import reverse_lazy
url_patterns = [
        path('your path' , views.CommentDeleteView.as_view(success_url=reverse_lazy('the path you are directing to')) , name = 'ad_comment_delete']

相关问题