How to call multiple API and render the response of all those API in a template in django

I tried rendering the response of multiple API in a single template but since once one view can we passed in the path, i am unable to think of what i can do

I tried keeping the two functions to call the API inside a class but i got GET 405 error

To call multiple APIs and render the response of all those APIs in a template in Django, you can create a view function that makes the API calls and passes the response data to the template as context.

Here's an example of how you can do this using the requests module to make the API calls:

from django.shortcuts import render
import requests

def view_function(request):
    # Make the first API call
    response1 = requests.get('https://api.example.com/endpoint1')
    data1 = response1.json()

    # Make the second API call
    response2 = requests.get('https://api.example.com/endpoint2')
    data2 = response2.json()

    # Make the third API call
    response3 = requests.get('https://api.example.com/endpoint3')
    data3 = response3.json()

    # Render the template and pass the API response data as context
    return render(request, 'template.html', {
        'data1': data1,
        'data2': data2,
        'data3': data3,
    })

In this example, view_function is the view function that makes the API calls and renders the template. The requests.get function is used to make the API calls, and the response.json method is used to parse the JSON response from the API. The render function is used to render the template and pass the API response data as context to the template.

In the template, you can access the API response data using the context variables that you passed to the template. For example, you can access the data from the first API call using {{ data1 }}, and the data from the second API call using {{ data2 }}.

First of all cant say anything without seeing the code but I think you can create a dictionary and pass it as context in response. Save the multiple api and pass it into dictionary

mydict = {
   "api1":api_data,
   "api2":api_data,
}

pass this as context in response

Back to Top