/ polls /处的NoReverseMatch反向,找不到带有参数'(1,)'的'detail'。尝试了1个模式:['民意调查/ <int:pk> /']

时间:2018-11-10 07:51:59

标签: python django django-views django-urls

尝试启动服务器并出现错误行,如

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 %}

我已经尝试了很多次,但是我没有解决方案,请帮帮我。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

对于使用正则表达式的人,下面的类似内容将起作用;

function groupArray(arr) {
  const map = {};
  arr.forEach(element => {
    const [id, type, code, priority] = element;
    map[code] = Array.isArray(map[code]) ? [...map[code], element] : [element];
  });
  return map;
}

const type_zero_group = groupArray(type_zero)
const type_one_group = groupArray(type_one)
const type_two_group = groupArray(type_two)