Django不正确的URL NoReverseMatch



URL

urlpatterns = [
#PAPER PROJECTS
path('create-paper-project/', PTCreateView.as_view(), name='pt-create'),
path('list-paper-projects/', PTListView.as_view(), name='pt-list'),
path('pproj<str:pk>/', PTDetailView.as_view(), name='pt-detail'),
path('pprojp<str:pk>/update/', PTUpdateView.as_view(), name='pt-update'),
path('pproj<str:pk>/delete/', PTDeleteView.as_view(), name='pt-delete'),
]

查看

class PTCreateView(generic.CreateView):
model = PaperTool
template_name = 'papertools/pt_create.html'
success_url = '/paper-tools/list-paper-projects/'
fields = ['title']
class PTListView(generic.ListView):
model = PaperTool
template_name = 'papertools/pt_list.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
class PTDetailView(generic.DetailView):
model = PaperTool
template_name = 'papertools/pt_detail.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
class PTUpdateView(generic.UpdateView):
model = PaperTool
fields = ['title']
template_name = 'papertools/pt_update.html'
success_url = reverse_lazy('paper:pt-detail', kwargs={'pk': model.pk})
class PTDeleteView(generic.DeleteView):
model = PaperTool
template_name = 'papertools/pt_delete.html'
success_url = reverse_lazy('paper:pt-detail')

HTML

<a class="nav-link" href="{% url 'pt-detail' object_list.paper.pk %}">
Paper Project Home
</a>

我不断地得到以下错误:;找不到参数为"(",)"的"pt detail"的反转。尝试了1种模式:"纸张工具/propj(?P[^/]+)/$']";在HTML部分中,如果我将";object_list.paper.pk";只有pk的部分(比如6)。这很好用。我甚至可以将其他pk插入URL。我不明白为什么这是一个错误。StackOverflow上似乎也有类似的问题,但没有一个完全像这样。

您的urls.py中没有正确配置名为pt-detail的路径您在您的模板中调用它=";{%url"pt-detail"object_list.paper.pk%}">

你只需要在你的urls.py 中做一个小的调整

像这样。。

path('pt-detail/<str:pk>', PTDetailView.as_view(), name='pt-detail'),

不是这样的。。。

path('pproj<str:pk>/', PTDetailView.as_view(), name='pt-detail'),

最新更新