我试图弄清楚为什么我收到以下错误:
NoReverseMatch at /catalog/projects/delete/1/
Reverse for 'project_detail' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['catalog/projects/(?P<pk>[0-9]+)/$']
这是目录应用程序的网址。我认为问题出在此处,但我似乎无法弄清楚
网址.py
urlpatterns = [
path('projects/', views.ProjectListView.as_view(), name='list'),
path('projects/<int:pk>/', views.ProjectDetailView.as_view(), name='project_detail'),
path('projects/new/', views.ProjectCreateView.as_view(), name='project_new'),
path('projects/edit/<int:pk>/', views.ProjectUpdateView.as_view(), name='project_edit'),
path('projects/delete/<int:pk>/', views.ProjectDeleteView.as_view(), name='project_delete')
]
视图.py
class HomeView(TemplateView):
template_name = "home.html"
def get_context_data(self, **kwargs):
context = super(HomeView, self).get_context_data(**kwargs)
context = {
'projects': Project.objects.all(),
'num_projects': Project.objects.all().count(),
'num_ongoing_projects': Project.objects.filter(status__exact='OG').count(),
'num_clients': Client.objects.count(),
}
return context
class ProjectListView(ListView):
context_object_name = 'projects'
model = Project
paginate_by = 10
class ProjectDetailView(DetailView):
model = Project
context_object_name = 'project_detail'
template_name = 'catalog/project_detail.html'
class ProjectCreateView(CreateView):
# login_url = '/login/'
form_class = ProjectForm
model = Project
redirect_field_name = 'catalog/project_detail.html'
class ProjectUpdateView(UpdateView):
fields=('project_title', 'location', 'status', 'start_date', 'end_date')
# form_class = ProjectForm
model = Project
redirect_field_name = 'catalog/project_detail.html'
class ProjectDeleteView(DeleteView):
model = Project
success_url = reverse_lazy('catalog:list')
编辑按钮工作正常,但删除按钮没有。它产生该错误代码
project_detail.html
{% extends "catalog/catalog_base.html" %}
{% block content %}
<h1>Welcome to Project Detail Page</h1>
<h2>Project details:</h2>
<p><strong>Project Number:</strong> {{project_detail.project_number}}</p>
<p><strong>Project Title:</strong> {{project_detail.project_title}}</p>
<p><strong>Customer:</strong> {{project_detail.client}}</p>
<p><strong>Further For:</strong> {{project_detail.further_for}}</p>
<p><strong>Location:</strong> {{project_detail.location}}</p>
<p><strong>Status:</strong> {{project_detail.status}}</p>
<p><strong>Start Date:</strong> {{project_detail.start_date}}</p>
<p><strong>End Date:</strong> {{project_detail.end_date}}</p> <br>
<p><strong>Summary:</strong> {{project_detail.summary}}</p>
<hr>
<div class="container">
<a class="btn btn-primary" href="{% url 'catalog:project_edit' pk=project_detail.pk %}">
<i class="fas fa-pen"></i>
</a>
<a class="btn btn-warning" href="{% url 'catalog:project_delete' pk=project_detail.pk %}">
<span class="fas fa-trash" aria-hidden="true"></span>
</a>
</div>
{% endblock %}
project_form.html
{% extends "catalog/catalog_base.html" %}
{% block content %}
{% load crispy_forms_tags %}
<div class="col-sm-6">
<h1>Create New Project</h1>
<h3><em>Enter project details:</em></h3><br>
<form id="create" method="POST">
{{ form|crispy }}
{% csrf_token %}
<input type="submit" class="btn btn-primary" value="Create Project">
</form>
</div>
{% endblock %}
project_confirm_delete.html
{% extends "catalog/catalog_base.html" %}
{% block content %}
<h1>Delete {{project.project_number}}?</h1>
<form method="POST">
{% csrf_token %}
<input type="submit" class="btn btn-danger" value="Delete">
<a href="{% url 'catalog:project_detail' pk=project_detail.pk %}">Cancel</a>
</form>
{% endblock %}