python - NoReverseMatch at / Reverse for 'detail' with arguments '('',)' 未找到。尝试了 1 种模式:
问题描述
我正在逐步遵循 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/)views.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>
解决方案
QuerySet
是latest_question_list
在模板中传递的,这是一个对象的集合,Question
因此您应该迭代:
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
推荐阅读
- html - 如何在导航中垂直居中徽标?
- python - 在一次试验中更新数据并再次使用更新数据的功能
- django - 如何正确更新 django 中的用户配置文件
- python - Beautifulsoup 是否有可以删除所有空格的功能
- node.js - 我在使用 npm install express 时遇到问题
- docker - 无法连接到 docker 容器
- node.js - 业务逻辑nodejs放在哪里
- npm - 端口 4200 已在使用中。使用“--port”指定不同的端口
- python - ValueError:检查目标时出错:预期dense_1的形状为(16、1、2)但得到的数组形状为(2、1、1)
- javascript - 使用 JavaScript 在数组中本地输出重复元素