read api from django 1

read api from django

from django.shortcuts import render
import requests

def github(request):
    user = {}
    if 'username' in request.GET:
        username = request.GET['username']
        url = 'https://api.github.com/users/%s' % username
        response = requests.get(url)
        user = response.json()
    return render(request, 'core/github.html', {'user': user})

Here is what the above code is Doing:
1. We’re importing the requests library.
2. We’re defining a view called github.
3. We’re checking if the username is in the request.GET dictionary.
4. If it is, we’re assigning it to a variable called username.
5. We’re creating a URL to the GitHub API.
6. We’re making a GET request to the URL.
7. We’re converting the response to JSON.
8. We’re passing the JSON response to the template.

Similar Posts