【问题标题】:Reverse for 'delete_post' with arguments '('',)' not found. 1 pattern(s) tried: ['article/(?P<pk>[0-9]+)/remove$']未找到带有参数 '('',)' 的 'delete_post' 的反向操作。尝试了 1 种模式:['article/(?P<pk>[0-9]+)/remove$']
【发布时间】:2022-01-13 12:12:05
【问题描述】:

enter image description herei 正在创建一个简单的博客站点,我创建了 delete7edit 按钮,但是当我尝试重置应用程序并转到那里时,我得到了这个错误(标题)

这是我的代码view.py:

from django.views.generic import ListView
from . models import Article
from django.views.generic import ListView, DetailView, CreateView, UpdateView, 
DeleteView
from .forms import PostForm, EditForm
from django.urls import reverse_lazy



class ArticleListView(ListView):
    model = Article
    template_name = 'home.html'


class ArticleDetailView(DetailView):
    model = Article
    template_name = 'detail.html'


class AddPostView(CreateView):
    model = Article
    form_class = PostForm
    template_name = 'add_post.html'
    #fields = '__all__'
    #fields = ('title', 'body')


class UpdatePostView(UpdateView):
    model = Article
    form_class = EditForm
    template_name = 'update_post.html'
    #fields = ['title', 'title_tag', 'body']


class DeletePostView(DeleteView):
    model = Article
    template_name = 'delete_post.html'
    success_url = reverse_lazy('home')

urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.ArticleListView.as_view(), name='home'),
    path('article/<int:pk>', views.ArticleDetailView.as_view(), name='detail'),
    path('add_post/', views.AddPostView.as_view(), name='add_post'),
    path('article/edit/<int:pk>', views.UpdatePostView.as_view(), name='update_post'),
    path('article/<int:pk>/remove', views.DeletePostView.as_view(), name='delete_post'),
]

模板 delete_post.html:

{% extends 'base.html' %}
{% block title %}Delete Blog Post{% endblock %}
{% block content %}


<h1>Delete Post...</h1>
<br/><br/>
<hr3>Delete: {{ post.title }}</hr3>

<br/>
<div class="mb-3">
    <form method="POST">{% csrf_token %}
        <strong>Are you sure?</strong><br/><br/>
         <button class="btn btn-secondary">Delete Post!</button>
</div>


{% endblock %}

模板 update_post.html:

{% extends 'base.html' %}
{% block title %}Edit Blog Post{% endblock %}
{% block content %}


<h1>Update Post...</h1>
<br/><br/>
<div class="mb-3">
    <form method="POST">{% csrf_token %}
        {{ form.as_p }}
        <button class="btn btn-secondary">Update</button>
</div>


{% endblock %}

请帮帮我,我是 django 的新手,我还不知道 ----------------------------- ------------------------------

【问题讨论】:

  • 也分享您的模板
  • 我有各种各样的模板,只发送那些删除和编辑?
  • 是的,只分享相关的。
  • 完成,我添加了它们
  • 什么时候发生错误,点击“删除帖子!”正确的?还是在加载文章详细信息页面时?

标签: python django django-models django-views django-templates


【解决方案1】:

您的删除网址 article/&lt;int:pk&gt;/remove 但我在图片中看到您输入了 article/1/

path('article/<int:pk>/remove', views.DeletePostView.as_view(), name='delete_post')

你需要输入article/1/remove

【讨论】:

  • 不起作用 --
  • 现在显示什么
  • NoReverseMatch at / Reverse for 'delete_post' 未找到参数 '(1,)'。尝试了 1 种模式:['article/1/remove$']
猜你喜欢
  • 2021-06-13
  • 2020-02-06
  • 2017-11-05
  • 2019-09-07
  • 2020-08-16
  • 1970-01-01
  • 2023-03-28
  • 2021-04-24
  • 1970-01-01
相关资源
最近更新 更多