首页 文章

NoReverseMatch at / polls / Reverse for 'detail',找不到参数'(1,)' . 1种模式尝试过:['polls/<int:pk>/']

提问于
浏览
0

试图启动服务器并获得错误行

NoReverseMatch at /polls/
Reverse for 'detail' with arguments '(1,)' not found. 1 pattern(s) tried: ['polls/<int:pk>/']

我的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."""
        return Question.objects.order_by('-pub_date')[:5]


class DetailView(generic.DetailView):
    model = Question
    template_name = 'polls/detail.html'


class ResultsView(generic.DetailView):
    model = Question
    template_name = 'polls/results.html'

索引,结果和详细信息的视图

我的urls.py

from django.conf.urls import url

from . import views


app_name = 'polls'

urlpatterns = [
    url('', views.IndexView.as_view(), name='index'),
    url(r'^<int:pk>/', views.DetailView.as_view(),name='detail'),
    url(r'^<int:pk>/results/', views.ResultsView.as_view(), name='results'),
    url(r'^<int:question_id>/vote/', views.vote, name='vote'),
]

我的index.html

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

我已经尝试了很多次,但我没有得到解决方案,请帮助我 .

1 回答

  • 0

    您正在混合 url() 语法(使用正则表达式)和Django 2.0中的新 path() 语法 .

    确保您使用的是与Django版本匹配的教程(例如Django 2.1或例如Django 1.11),然后确保您的urls.py与教程中的代码完全匹配 .

相关问题