【发布时间】:2020-06-10 22:43:14
【问题描述】:
我正在逐步学习 django 教程,但无法找出我收到此错误的原因:
NoReverseMatch at /polls/
Reverse for 'detail' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<pk>[0-9]+)/$']
我的代码(基本上都是从教程复制的 - https://docs.djangoproject.com/en/3.0/intro/tutorial03/) 视图.py:
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
"""
Return the last five published questions (not including those set to be
published in the future).
"""
return Question.objects.filter(
pub_date__lte=timezone.now()
).order_by('-pub_date')[:5]
投票/urls.py
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
民意调查/index.html
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
【问题讨论】:
-
上下文是
latest_question_list而不是question。 -
你的detailview功能!!!请