How to integrate an API in Django?

Please help us to integrate an API in django.

I suppose you are aware of django url.py, views.py and models.py files. So, I'm going to show you how to use python inbuilt client to integrate API in views.py file. How to import the package and use it, please see below example:

#### views.py ####

import requests

def testapi(request):
    response = requests.get("https://api.github.com/users")
    response = response.json()
    context = {"response":response}
    return render(request, "tnews/apiusers.html", context)

 

Here you will get the records from the given url above and then convert it into json format so, it can be accessed easily and show in template file and testapi is a function that will be linked with a URL from urls.py file. "tnews/apiusers.html" is the template file name where records will be available that can any thing as per your choice.

If you have any confusion, write in the comment box and will get back to you.