0

I am trying to make a get request with an ID argument in django as the doc says in https://docs.djangoproject.com/en/3.2/topics/class-based-views/ but I can't seem to make it work.

The request is arriving in the backend as expected. "GET /api/acordo-parcela/1/ HTTP/1.1" 200 348 where 1 is my ID. but even after creating a new urlpattern it does not reach the view class

The urlpattern: path('/acordo-parcela/<int:clienteId>/', GetParcelas.as_view()),

The GetParcelas class:

class GetParcelas(APIView):
    print("test")
    def get(self, request, clienteId):
        print("inside")
        return Response(clienteId)

I have read the docs on https://docs.djangoproject.com/en/3.2/topics/http/urls/ and tried with views.AcordoParcelaViewSet (a viewset I created accordingly) but keep getting a 'has no attribute' error. What is the easyer way to make this get request with an argument?

Luis Moita
  • 47
  • 1
  • 8
  • You need to have self as the first parameter in the get function since it is an internal function of the class. Read more about it here https://www.geeksforgeeks.org/self-in-python-class/ . – Dexter Aug 02 '21 at 14:45
  • Have tried both with and without the self argument (self, request, clienteId) as so. both with GetParcelas.as_view() and views.AcordoParcelaViewSet but still not calling my function – Luis Moita Aug 02 '21 at 14:51
  • If you add self then it would work, I just tested the code, If you can share your view.py and urls.py then only people can tell because. The code looks perfectly fine except for the fact that you need to add self and return response from get function. – Dexter Aug 02 '21 at 15:04
  • I have edited the code as it is currently with the changes, but still not working. I can't share the full doc but I asure you I am importing everything accordingly. – Luis Moita Aug 02 '21 at 15:12
  • Can you share the errors/ complete traceback – Brian Destura Aug 03 '21 at 04:22
  • It gave me no errors, it simply didn't enter the function as if it was never called. I erased both parts of the code and rewrote with different names for variables and functions and it worked. Gues I'll never know what the problem was – Luis Moita Aug 03 '21 at 19:04

0 Answers0