$'],python,django"/>

首页 > 解决方案 > NoReverseMatch:未找到带有参数“(1,)”的“详细信息”的反向。尝试了 1 种模式:['polls\\/\\$']

问题描述

我正在关注 Traversy Media 的民意调查应用程序 Django 教程,当我在接近尾声时运行我的代码时,我突然收到标题中所述的错误:为什么找不到详细信息?

视图.py

from django.shortcuts import render

from .models import Question, Choice 

# Get questions and display them 

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)


#Show specific question and choices 

def detail(request, question_id):
    try:
        question = Question.objects.get(pk=question_id)
    except Question.DoesNotExist:
        raise Http404("Question does not exist")
    return render(request, 'polls/results.html', { 'question': question })

# Get questions and display results

def results(request, question_id):
        question = get_object_or_404(Question, pk=question_id)
        return render(request, 'polls/results.html', { 'question': question })

urls.py(民意调查)

from django.urls import path

from . import views 

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

urls.py(民意调查)

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

索引.html

{% extends 'base.html' %}
{% block content %}
    <h1 class="text-center mb-3">Poll Questions</h1>
    {% if latest_question_list %}
        {% for question in latest_question_list %}
            <div class="card mb-3">
                <div class="card-body">
                    <p class="lead">{{ question.question_text }}</p>
                    <a href="{% url 'polls:detail' question.id %}" class="btn btn-primary btn-sm">Vote Now</a>
                    <a href="{% url 'polls:results' question.id %}" class="btn btn-secondary btn-sm">Results</a>
                </div>
            </div>
        {% endfor %}
    {% else %}
        <p>No polls available</p>
    {% endif %}
{% endblock %}

base.html

<!DOCTYPE html> 
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <link 
            rel="stylesheet"
            href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" 
            integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" 
            crossorigin="anonymous" 
        />
        <title>Pollster {% block title %}{% endblock %}</title>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-md-6 m-auto">
                    {% block content %}{% endblock %}
                </div>
            </div>
        </div>
    </body>
</html>

任何见解都将不胜感激,自从我被困在这里以来,我根本无法取得进展。提前致谢!

标签: pythondjango

解决方案


您不应该在路径的路径转换器部分放置斜杠:

app_name = 'polls'

urlpatterns = [
    path('', views.index, name='index'),
    path('<int:question_id>/', views.detail, name='detail'),
    path('<int:question_id>/results/', views.results, name='results'),
]

推荐阅读