Error Expected view to be called with a URL keyword argument named pk

0 votes

I'm having simple test as:

def test_patient_detail_api_opens(self):
    factory = APIRequestFactory()
    view =PatientDetailApi.as_view()
    request = factory.get(reverse('api_pacjent', kwargs={'pk' :1}))
    force_authenticate(request, user=self.user)
    response = view(request)
    self.assertEqual(response.status_code, 200)

This test fails with the following message:

AssertionError: Expected view PatientDetailApi to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly.

Here's the relevant code:

the 'main' url.py:

urlpatterns = [
    url(r'^pacjent/', include('pacjent.urls')),
] 

pacjent.urls looks like this:

url(r'^api/szczegoly/(?P<pk>\d+)/$', PatientDetailApi.as_view(), name="api_pacjent"),

And PatientDetailApi is this:

class PatientDetailApi(generics.RetrieveUpdateAPIView):
    model = Patient
    serializer_class = PatientDetailsSerializer
    queryset = Patient.objects.all()

    authentication_classes = (SessionAuthentication, BasicAuthentication)
    permission_classes = (IsAuthenticated,) 

Can somebody please explain why this error occurs?

Jul 1, 2020 in Python by kartik
• 37,520 points
1,835 views

1 answer to this question.

0 votes

Hello @kartik,

View functions are called with the request and the arguments from the URL. So pass them:

response = view(request, pk=1)

Hope it works!!

Thank You!!

answered Jul 1, 2020 by Niroj
• 82,760 points

Related Questions In Python

0 votes
3 answers

TypeError: Index(...) must be called with a collection of some kind, 'Email' was passed

It is not working because your code ...READ MORE

answered May 12, 2020 in Python by anonymous

edited May 13, 2020 by Gitika 28,874 views
+1 vote
10 answers

How to fix this? ValueError: invalid literal for int() with base 10 error in Python

Just for the record: >>> int('55063.000000') Traceback (most recent ...READ MORE

answered Aug 17, 2020 in Python by pakainfo
• 200 points
279,548 views
0 votes
1 answer

How to print a message or the error if a file is not found?

To print the message that file is not ...READ MORE

answered Jan 2, 2019 in Python by Omkar
• 69,150 points
279 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,760 points
831 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,760 points
346 views
0 votes
1 answer

How to pretty-print a numpy.array without scientific notation and with given precision?

Hii @kartik, The numpy arrays have the method round(precision) which ...READ MORE

answered Apr 14, 2020 in Python by Niroj
• 82,760 points
1,270 views
0 votes
1 answer

How to make a single legend for many subplots with matplotlib?

Hello @kartik, There is  nice function get_legend_handles_labels() you can call ...READ MORE

answered May 7, 2020 in Python by Niroj
• 82,760 points
2,253 views