Home > Software design >  Reverse for 'exam-detail' with arguments '(4,)' not found. 1 pattern(s) tried
Reverse for 'exam-detail' with arguments '(4,)' not found. 1 pattern(s) tried

Time:12-28

When rendering a template django returns this error:

NoReverseMatch at /user/admin/package/1/
Reverse for 'exam-detail' with arguments '(4,)' not found. 1 pattern(s) tried: ['user/(admin|staff)/exam/(?P<pk>[0-9] )/$']

html file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    {{object.name}}
    {{object.price}}
    {% for exam in object.exams.all %}
        <p><a href="{% url 'exam-detail' exam.pk %}">{{exam.name}}</a></p>
    {% endfor %}
</body>
</html>

urls.py:

re_path(r'^(admin|staff)/exam/(?P<pk>[0-9] )/$',ExamDetail.as_view(),name='exam-detail'),

When I change the path to the following the error is resolved:

path('admin/exam/<int:pk>/',ExamDetail.as_view(),name="exam-detail"),

CodePudding user response:

Your url r'^(admin|staff)/exam/(?P<pk>[0-9] )/$' needs two arguments "admin" or "staff" and a primary key.

In the html, you pass only one argument.

<p><a href="{% url 'exam-detail' 'admin' exam.pk %}">{{exam.name}}</a></p>
<p><a href="{% url 'exam-detail' 'staff' exam.pk %}">{{exam.name}}</a></p>
  • Related