如何在django中使用我的模式显示博客文章内容并删除它?

如何在django中使用我的模式显示博客文章内容并删除它?,django,bootstrap-modal,Django,Bootstrap Modal,我在django中为我的博客主页创建了一个引导模式,我想用它来删除帖子,并在删除之前显示帖子内容 触发模式的html如下所示: <button type="button" data-toggle="modal" data-target="#delete" class="btn btn-danger">Delete</button> class PostDeleteView(LoginRequ

我在django中为我的博客主页创建了一个引导模式,我想用它来删除帖子,并在删除之前显示帖子内容

触发模式的html如下所示:

<button type="button" data-toggle="modal" data-target="#delete" class="btn btn-danger">Delete</button>
class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
    model = Post
    template_name = 'blog/home.html'
    context_object_name = 'post'
    success_url = reverse_lazy('blog_home')
My URL.py文件:

urlpatterns = [
    path('', PostListView.as_view(), name='stream_home'),
    path('post/new/', PostCreateView.as_view(), name='post_create'),
    path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post_update'),
    path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post_delete'),
]
尝试加载页面时,我会看到以下错误:

Reverse for 'post_delete' with arguments '('',)' not found. 1 pattern(s) tried: ['post/(?P<pk>[0-9]+)/delete/$']
未找到带有参数“(“”,)”的“post_delete”的反向。尝试了1个模式:[“post/(?P[0-9]+)/delete/$”] 如果我删除删除按钮,页面将加载,点击时会显示模式,但我的{{post.content}}标记无法显示。请从中帮助…

,我认为您需要为DeleteView使用表单

<form method="post">
    {% csrf_token %}
    <p>Are you sure you want to delete?</p>
    <input type="submit" class="btn btn-danger btn-outline" value="Yes, Delete">
    <button type="button" data-dismiss="modal" class="btn btn-secondary btn-outline">Cancel</button>
</form>

{%csrf_令牌%}
您确定要删除吗

取消

那个按钮好像没什么作用。可能会将其更改为链接回Listview或其他内容

也可以发布models.py。更新显示modelsIt看起来好像找不到您的post对象。帖子id是否显示在url中?我只收到一个NoReverseMatch,所以我甚至看不到url。但我相信你是对的,模态不能识别物体。我尝试将href重新定位到模式触发器按钮,但是这只会导致更奇怪的错误检查,以查看您的post对象是否存在于django admin或数据库中。你能在listview或update视图中看到它吗?谢谢,仍然不工作。我仍然收到同样的消息:-(看起来你的url和视图中没有定义“post\u detail”(在模型中的get\u absolute\u url方法中),我已经解决了这个问题。我的模式包含在我的博客帖子的for循环之外。现在一切正常了!非常感谢你的帮助
Reverse for 'post_delete' with arguments '('',)' not found. 1 pattern(s) tried: ['post/(?P<pk>[0-9]+)/delete/$']
<form method="post">
    {% csrf_token %}
    <p>Are you sure you want to delete?</p>
    <input type="submit" class="btn btn-danger btn-outline" value="Yes, Delete">
    <button type="button" data-dismiss="modal" class="btn btn-secondary btn-outline">Cancel</button>
</form>