【问题标题】:NoReverseMatch: Reverse for 'detail' with arguments '(1,)' not found. 1 pattern(s) tried: ['polls\\/\\<int\\:question_id\\/\\>$']NoReverseMatch:未找到带有参数“(1,)”的“详细信息”的反向。尝试了 1 种模式:['polls\\/\\<int\\:question_id\\/\\>$']
【发布时间】:2020-07-02 21:40:31
【问题描述】:

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

views.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(pollster)

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

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

index.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>

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

【问题讨论】:

标签: python django


【解决方案1】:

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

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'),
]

【讨论】:

  • 非常感谢!
猜你喜欢
  • 2019-05-10
  • 2019-06-03
  • 1970-01-01
  • 1970-01-01
  • 2019-06-01
  • 2020-11-06
  • 2018-11-18
  • 1970-01-01
相关资源
最近更新 更多