如何修复NoReverseMatch错误当我尝试将我的代码转换为基于类的代码时?

huangapple go评论53阅读模式
英文:

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 &#39;delete&#39; with arguments &#39;(&#39;&#39;,)&#39; not found.
1 pattern(s) tried: [&#39;dashboard/leads/(?P&lt;pk&gt;[0-9]+)/delete/$&#39;]

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[&#39;form&#39;] = AddCommentForm()
def get_queryset(self):
queryset = super(LeadDetailView, self).get_queryset()
return queryset.filter(created_by=self.request.user, pk=self.kwargs.get(&#39;pk&#39;))
class LeadDeleteView(DeleteView):
model = Lead
success_url = reverse_lazy(&#39;leads:list&#39;)
@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(&#39;pk&#39;))
def get(self, request, *args, **kwargs):
return self.post(request, *args, **kwargs)
class LeadUpdateView(UpdateView):
model = Lead
fields = (&#39;name&#39;, &#39;email&#39;, &#39;description&#39;, &#39;priority&#39;, &#39;status&#39;,)
success_url = reverse_lazy(&#39;leads:list&#39;)
@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[&#39;title&#39;] = &#39;Edit lead&#39;
return context
def get_queryset(self):
queryset = super(UpdateView, self).get_queryset()
return queryset.filter(created_by=self.request.user, pk=self.kwargs.get(&#39;pk&#39;))

urls.py

from django.urls import path
from . import views
app_name = &#39;leads&#39;
urlpatterns = [
path(&#39;&#39;, views.LeadListView.as_view(), name=&#39;list&#39;),
path(&#39;&lt;int:pk&gt;/&#39;, views.LeadDetailView.as_view(), name=&#39;detail&#39;),
path(&#39;&lt;int:pk&gt;/delete/&#39;, views.LeadDeleteView.as_view(), name=&#39;delete&#39;),
path(&#39;&lt;int:pk&gt;/edit/&#39;, views.LeadUpdateView.as_view(), name=&#39;edit&#39;),
path(&#39;&lt;int:pk&gt;/convert/&#39;, views.ConvertToClientView.as_view(), name=&#39;convert&#39;),
path(&#39;add/&#39;, views.LeadCreateView.as_view(), name=&#39;add&#39;),
]
{% extends &#39;core/base.html&#39; %}
{% block title %}{{ lead.name }}{% endblock %}
{% block content %}
&lt;div class=&quot;py-4 px-6&quot;&gt;
&lt;h1 class=&quot;mb-4 text-xl&quot;&gt;{{ lead.name }}&lt;/h1&gt;
{% if lead.description %}
&lt;p class=&quot;font-semibold&quot;&gt;Description&lt;/p&gt;
&lt;p class=&quot;mb-2 &quot;&gt; {{ lead.description }}&lt;/p&gt;
{% endif %}
&lt;p class=&quot;font-semibold&quot;&gt;Priority&lt;/p&gt;
&lt;p class=&quot;mb-2 &quot;&gt;{{ lead.get_priority_display }}&lt;/p&gt;
&lt;p class=&quot;font-semibold&quot;&gt;Status&lt;/p&gt;
&lt;p class=&quot;mb-2 &quot;&gt;{{ lead.get_status_display }}&lt;/p&gt;
&lt;p class=&quot;font-semibold&quot;&gt;Created at&lt;/p&gt;
&lt;p class=&quot;mb-2 &quot;&gt;{{ lead.created_at|date:&quot;M-d-Y H:i&quot; }}&lt;/p&gt;
&lt;p class=&quot;font-semibold&quot;&gt;Modified at&lt;/p&gt;
&lt;p class=&quot;mb-6 &quot;&gt;{{ lead.modified_at }}&lt;/p&gt;
&lt;hr class=&quot;my-4&quot;&gt;
&lt;form id=&quot;lead_delete_form&quot; method=&quot;post&quot; action=&quot;{% url &#39;leads:delete&#39; lead.id %}&quot; &gt;
{% csrf_token %}
&lt;/form&gt;
&lt;a href=&quot;{% url &#39;leads:convert&#39; lead.id %}&quot; class=&quot;py-2 px-4 rounded-xl bg-blue-500 text-white&quot;&gt;Convert to client&lt;/a&gt;
&lt;a href=&quot;{% url &#39;leads:edit&#39; lead.id %}&quot; class=&quot;py-2 px-4 rounded-xl bg-teal-500 text-white&quot;&gt;Edit&lt;/a&gt;
&lt;a href=&quot;#&quot; onclick=&quot;document.getElementById(&#39;lead_delete_form&#39;).submit();&quot; class=&quot;py-2 px-4 rounded-xl bg-red-500 text-white&quot;&gt;Delete&lt;/a&gt;
&lt;hr class=&quot;my-4&quot;&gt;
&lt;h2 class=&quot;text-2xl&quot;&gt;Comments&lt;/h2&gt;
&lt;form method=&quot;get&quot; action=&quot;.&quot;&gt;
{% csrf_token %}
{{ form.as_p }}
&lt;button class=&quot;py-2 px-4 rounded-xl bg-blue-500 text-white&quot;&gt;Submit&lt;/button&gt;
&lt;/form&gt;
&lt;hr class=&quot;my-4&quot;&gt;
&lt;a href=&quot;{% url &#39;leads:list&#39; %}&quot;&gt;Back to Leads&lt;/a&gt;
&lt;/div&gt;
{% endblock %}

答案1

得分: 2

基于您的 "urls.py" 文件,删除视图需要 pk 参数,错误提示您在生成URL时未正确传递 pk 参数。

要解决此问题,您应确保在使用 reversereverse_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(&#39;leads:detail&#39;, kwargs={&#39;pk&#39;: self.object.pk})

huangapple
  • 本文由 发表于 2023年6月1日 07:11:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定