I have a django rest API which gives list of students to certain authentic users. It needs to be called from a django view. Currently I am using requests library with token authentication inside get_context_data as shown below:
import requests
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
token,created = Token.objects.get_or_create(user=self.request.user)
reverse_url = 'http://127.0.0.1:8000/api/students/'
response = requests.get(
reverse_url,
headers={'Authorization': 'Token {}'.format(token)}
)
context['object'] = response.json()
return context
Is this the right way of doing it Thanks