英文:
how do I fix NoReverseMatch error when i trying convert to class base my code?
问题
I have some code and when it executes, it throws a NoReverseMatch, saying:
这段代码在执行时出现了NoReverseMatch错误,错误信息如下:
NoReverseMatch at /dashboard/leads/6/
Reverse for 'delete' with arguments ('',) not found.
1 pattern(s) tried: ['dashboard/leads/(?P<pk>[0-9]+)/delete/$']
view.py
class LeadDetailView(DetailView):
model = Lead
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = AddCommentForm()
def get_queryset(self):
queryset = super(LeadDetailView, self).get_queryset()
return queryset.filter(created_by=self.request.user, pk=self.kwargs.get('pk'))
class LeadDeleteView(DeleteView):
model = Lead
success_url = reverse_lazy('leads:list')
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
def get_queryset(self):
queryset = super(LeadDeleteView, self).get_queryset()
team = self.request.user.userprofile.active_team
return queryset.filter(team=team, pk=self.kwargs.get('pk'))
def get(self, request, *args, **kwargs):
return self.post(request, *args, **kwargs)
class LeadUpdateView(UpdateView):
model = Lead
fields = ('name', 'email', 'description', 'priority', 'status',)
success_url = reverse_lazy('leads:list')
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Edit lead'
return context
def get_queryset(self):
queryset = super(UpdateView, self).get_queryset()
return queryset.filter(created_by=self.request.user, pk=self.kwargs.get('pk'))
urls.py
from django.urls import path
from . import views
app_name = 'leads'
urlpatterns = [
path('', views.LeadListView.as_view(), name='list'),
path('<int:pk>/', views.LeadDetailView.as_view(), name='detail'),
path('<int:pk>/delete/', views.LeadDeleteView.as_view(), name='delete'),
path('<int:pk>/edit/', views.LeadUpdateView.as_view(), name='edit'),
path('<int:pk>/convert/', views.ConvertToClientView.as_view(), name='convert'),
path('add/', views.LeadCreateView.as_view(), name='add'),
]
{% extends 'core/base.html' %}
{% block title %}{{ lead.name }}{% endblock %}
{% block content %}
<div class="py-4 px-6">
<h1 class="mb-4 text-xl">{{ lead.name }}</h1>
{% if lead.description %}
<p class="font-semibold">Description</p>
<p class="mb-2 "> {{ lead.description }}</p>
{% endif %}
<p class="font-semibold">Priority</p>
<p class="mb-2 ">{{ lead.get_priority_display }}</p>
<p class="font-semibold">Status</p>
<p class="mb-2 ">{{ lead.get_status_display }}</p>
<p class="font-semibold">Created at</p>
<p class="mb-2 ">{{ lead.created_at|date:"M-d-Y H:i" }}</p>
<p class="font-semibold">Modified at</p>
<p class="mb-6 ">{{ lead.modified_at }}</p>
<hr class="my-4">
<form id="lead_delete_form" method="post" action="{% url 'leads:delete' lead.id %}" >
{% csrf_token %}
</form>
<a href="{% url 'leads:convert' lead.id %}" class="py-2 px-4 rounded-xl bg-blue-500 text-white">Convert to client</a>
<a href="{% url 'leads:edit' lead.id %}" class="py-2 px-4 rounded-xl bg-teal-500 text-white">Edit</a>
<a href="#" onclick="document.getElementById('lead_delete_form').submit();" class="py-2 px-4 rounded-xl bg-red-500 text-white">Delete</a>
<hr class="my-4">
<h2 class="text-2xl">Comments</h2>
<form method="get" action=".">
{% csrf_token %}
{{ form.as_p }}
<button class="py-2 px-4 rounded-xl bg-blue-500 text-white">Submit</button>
</form>
<hr class="my-4">
<a href="{% url 'leads:list' %}">Back to Leads</a>
</div>
{% endblock %}
英文:
I have some code and when it executes, it throws a No Reverse Match, saying:
What does this mean, and what can I do about it?
I m trying convert my code to class base and name space for my app.
I have some code and when it executes, it throws a NoReverseMatch, saying:
NoReverseMatch at /dashboard/leads/6/
Reverse for 'delete' with arguments '('',)' not found.
1 pattern(s) tried: ['dashboard/leads/(?P<pk>[0-9]+)/delete/$']
view.py
class LeadDetailView(DetailView):
model = Lead
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = AddCommentForm()
def get_queryset(self):
queryset = super(LeadDetailView, self).get_queryset()
return queryset.filter(created_by=self.request.user, pk=self.kwargs.get('pk'))
class LeadDeleteView(DeleteView):
model = Lead
success_url = reverse_lazy('leads:list')
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
def get_queryset(self):
queryset = super(LeadDeleteView, self).get_queryset()
team = self.request.user.userprofile.active_team
return queryset.filter(team=team, pk=self.kwargs.get('pk'))
def get(self, request, *args, **kwargs):
return self.post(request, *args, **kwargs)
class LeadUpdateView(UpdateView):
model = Lead
fields = ('name', 'email', 'description', 'priority', 'status',)
success_url = reverse_lazy('leads:list')
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Edit lead'
return context
def get_queryset(self):
queryset = super(UpdateView, self).get_queryset()
return queryset.filter(created_by=self.request.user, pk=self.kwargs.get('pk'))
urls.py
from django.urls import path
from . import views
app_name = 'leads'
urlpatterns = [
path('', views.LeadListView.as_view(), name='list'),
path('<int:pk>/', views.LeadDetailView.as_view(), name='detail'),
path('<int:pk>/delete/', views.LeadDeleteView.as_view(), name='delete'),
path('<int:pk>/edit/', views.LeadUpdateView.as_view(), name='edit'),
path('<int:pk>/convert/', views.ConvertToClientView.as_view(), name='convert'),
path('add/', views.LeadCreateView.as_view(), name='add'),
]
{% extends 'core/base.html' %}
{% block title %}{{ lead.name }}{% endblock %}
{% block content %}
<div class="py-4 px-6">
<h1 class="mb-4 text-xl">{{ lead.name }}</h1>
{% if lead.description %}
<p class="font-semibold">Description</p>
<p class="mb-2 "> {{ lead.description }}</p>
{% endif %}
<p class="font-semibold">Priority</p>
<p class="mb-2 ">{{ lead.get_priority_display }}</p>
<p class="font-semibold">Status</p>
<p class="mb-2 ">{{ lead.get_status_display }}</p>
<p class="font-semibold">Created at</p>
<p class="mb-2 ">{{ lead.created_at|date:"M-d-Y H:i" }}</p>
<p class="font-semibold">Modified at</p>
<p class="mb-6 ">{{ lead.modified_at }}</p>
<hr class="my-4">
<form id="lead_delete_form" method="post" action="{% url 'leads:delete' lead.id %}" >
{% csrf_token %}
</form>
<a href="{% url 'leads:convert' lead.id %}" class="py-2 px-4 rounded-xl bg-blue-500 text-white">Convert to client</a>
<a href="{% url 'leads:edit' lead.id %}" class="py-2 px-4 rounded-xl bg-teal-500 text-white">Edit</a>
<a href="#" onclick="document.getElementById('lead_delete_form').submit();" class="py-2 px-4 rounded-xl bg-red-500 text-white">Delete</a>
<hr class="my-4">
<h2 class="text-2xl">Comments</h2>
<form method="get" action=".">
{% csrf_token %}
{{ form.as_p }}
<button class="py-2 px-4 rounded-xl bg-blue-500 text-white">Submit</button>
</form>
<hr class="my-4">
<a href="{% url 'leads:list' %}">Back to Leads</a>
</div>
{% endblock %}
答案1
得分: 2
基于您的 "urls.py" 文件,删除视图需要 pk
参数,错误提示您在生成URL时未正确传递 pk
参数。
要解决此问题,您应确保在使用 reverse
或 reverse_lazy
函数时正确提供了 pk
参数给 success_url
属性。
例如,您可以在您的 LeadDeleteView
中如下更新 success_url
属性:
success_url = reverse_lazy('leads:detail', kwargs={'pk': self.object.pk})
英文:
Based on your "urls.py" which requires the pk
argument on delete view, the error tells you that the pk
argument is not being passed correctly when generating the URL.
To fix this issue, you should ensure that the pk
argument is correctly provided to the success_url
attribute when using the reverse
or reverse_lazy
functions.
For example, you can update the success_url
attribute in your LeadDeleteView
as follows:
success_url = reverse_lazy('leads:detail', kwargs={'pk': self.object.pk})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论