I am trying to call an api made with Django Rest Framework drf. My case is that I want to call the api from another view get the response and display the data in a template. I have referred to this SO post, but could not make a successful call since my viewset requires authentication. Viewset works perfect if called using urls, the default way. Viewset is as below
class ViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows Objects to be viewed or edited.
"""
permission_classes = (permissions.IsAuthenticated,)
queryset = Myclass.objects.all()
serializer_class = MyclassSerializer
....
....
return response(json)
On calling this api from another view the response that I get is a 401 page from drf api.
ipdb>view = Myview.as_view({'get':'list'}
ipdb>obj=view(request, *args, **kwargs)
ipdb> obj.data
{u'detail': u'Authentication credentials were not provided.'}
ipdb>obj.has_header('Authentication')
False
I tried passing Authentication headers also, but I dont know whether its the right way.
view = MyViewSet.as_view({'get':'list'},{'token':'token'})
This returned an error argument token is not accepted. And I tried
view = MyViewSet.as_view({'get':'list'},Authentication={'token':'token'})
But this Also returned me errors. How is it possible to call an api viewset from another view by passing auth params?
TIA
MyClassSerializerand maybe some other parts from that view in your second view?