NoReverseMatch:未找到“详细信息”的反向。 (Django教程)

问题描述 投票:1回答:2

我一直在浏览Django 2教程。

我收到以下错误:

#Error:
#django.urls.exceptions.NoReverseMatch
#django.urls.exceptions.NoReverseMatch: Reverse for 'detail' not found. 'detail' is not a valid view function or pattern #name.

做了一些谷歌搜索,并确认我已将我的视图命名为“详细信息”并且还将我的应用命名为。

以下是我的代码。请告诉我有什么问题。我正在仔细阅读教程,但是这个问题出现了。如何修复它与教程相提并论?谢谢!

文件:mysite / polls / templates / polls / index.html

{% for question in latest_question_list %}
        <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
    {% endfor %}

mysite的/调查/ urls.py

app_name = 'polls'
urlpatterns = [
    path('', views.index, name='index'),
    # ex: /polls/
    # path('', views.index, name='index'),
    # ex: /polls/5/
    path('<int:question_id>/', views.detail, name='detail'),
    # ex: /polls/5/results/
    path('<int:question_id>/results/', views.results, name='results'),
    # ex: /polls/5/vote/
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

mysite的/调查/ views.py

def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_question_list': latest_question_list}
    return render(request, 'polls/index.html', context)

附加:mysite / urls.py

urlpatterns = [
    path('polls/', include('polls.urls', namespace='polls')),
    path('admin/', admin.site.urls),
]
django python-3.x django-2.0
2个回答
1
投票

您尚未在views.py文件中定义任何名为“detail”的函数。

添加此代码。

def detail(request, id):
    context = dict()
    return render(request, 'polls/index.html', context)

您还必须添加结果和投票功能。

从index.html文件中删除注释行。这些行中的语法不正确,Django也会在渲染之前尝试解析注释行。


0
投票

mysite/urls.py删除namespace,因为你已经指定app的app_name

或者你可以删除app_name并保留namespace(不确定这是否适用于Django 2.0,因为有some tweaks in app_name and namespace in this version)。

© www.soinside.com 2019 - 2024. All rights reserved.