[英]Django Improper 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>
我不斷收到以下錯誤:“未找到帶有 arguments '('',)' 的 'pt-detail' 的反向。嘗試了 1 個模式:['paper\-tools/pproj(?P[^/]+ )/$']" 在 HTML 部分中,如果我只用一個 pk 替換“object_list.paper.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'),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.