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

Question:

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"),
Asked By: Pexicade

||

Answers:

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>
Answered By: Lucas Grugru
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.