'list' object is not callable

715 views
Skip to first unread message

Bhoopesh sisoudiya

unread,
Feb 13, 2020, 10:17:12 PM2/13/20
to django...@googlegroups.com
Hi all,

How to resolve this exception in generic view of rest-framework- api
Exception===================
2020-02-14 08:30:33,523 - D:\futuredatapoints\futuredatapoints\logs\14022020.log - ERROR
ERROR:D:\futuredatapoints\futuredatapoints\logs\14022020.log:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Exception Start %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2020-02-14 08:30:33,523 - D:\futuredatapoints\futuredatapoints\logs\14022020.log - ERROR
2020-02-14 08:30:33,523 - D:\futuredatapoints\futuredatapoints\logs\14022020.log - ERROR
ERROR:D:\futuredatapoints\futuredatapoints\logs\14022020.log:Traceback (most recent call last):
  File "D:\futuredatapoints\env\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\futuredatapoints\env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "D:\futuredatapoints\env\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "D:\futuredatapoints\env\lib\site-packages\rest_framework\views.py", line 479, in dispatch
    request = self.initialize_request(request, *args, **kwargs)
  File "D:\futuredatapoints\env\lib\site-packages\rest_framework\views.py", line 380, in initialize_request
    authenticators=self.get_authenticators(),
  File "D:\futuredatapoints\env\lib\site-packages\rest_framework\views.py", line 274, in get_authenticators
    return [auth() for auth in self.authentication_classes]
  File "D:\futuredatapoints\env\lib\site-packages\rest_framework\views.py", line 274, in <listcomp>
    return [auth() for auth in self.authentication_classes]
TypeError: 'list' object is not callable

WARNING:django.request:Bad Request: /api/send/otp/


url====== ===========================================================
url(r'^api/send/data/$',UserViewSetCustom.as_view(),name='user-view-set-custom'),


 view generic class ================
class UserViewSetCustom(generics.CreateAPIView):
    """
    A simple CreateAPIView that for sending otp for user.
    """
    # permission_classes = (AllowAny,)
    authentication_classes = [TokenAuthentication,],
    queryset = User.objects.all()
    def create(selfrequest):
        print("*******")
        return Response(
            data=jsonResponse([],
            msg=Message.getMessage('NOT_ALLOWED'),
            status=status.HTTP_200_OK),
            status=status.HTTP_200_OK,
            )

Thanks
Bhoopesh Sisoudiya

Jorge Gimeno

unread,
Feb 13, 2020, 10:50:04 PM2/13/20
to django...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAAk3c1N0JBOFkF7tyMLJdMY3bh3H54r-QVh6_x-KW9peaRo9dA%40mail.gmail.com.

I did some experimenting in the Python shell.   What I found is this:

 jlgimeno@jlgimeno-Lenovo-G50-45  ~  python3.8
Python 3.8.1 (default, Dec 31 2019, 11:21:33)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [1]
>>> b = [1,]
>>> c = [1,],
>>> a
[1]
>>> b
[1]
>>> c
([1],)

The variables a and b are both lists with a single object, but c is different.  It is a tuple, with the first (and only) element is a list.  c is similar to your authentication_classes variable in your view because of that trailing comma which makes it a tuple.  As you can see in the last two lines of your traceback, django rest framework is trying to get a list of authenticators using a list comprehension, by calling each class that is an element of self.authentication_classes.  It's not getting a callable back, it's getting back a list.  Lists are not callables, and so Python raises an exception.

To fix this, remove the trailing comma in your authentication_classes variable like so:
authentication_classes = [TokenAuthentication,]

That should work. Hope this helped!

-Jorge

Bhoopesh sisoudiya

unread,
Feb 14, 2020, 12:09:04 AM2/14/20
to django...@googlegroups.com
Thanks, Jorge Gimeno this is working fine. 

Reply all
Reply to author
Forward
0 new messages