read api from django
- Import necessary modules in your Django view file:
python
from django.http import JsonResponse
import requests
- Define a function in the Django view to make a GET request to the API:
python
def get_api_data(request):
response = requests.get('https://api_url_here')
data = response.json()
return JsonResponse(data)
- Map the defined function to a URL in your Django urls.py:
```python from django.urls import path from .views import get_api_data
urlpatterns = [ path('api-data/', get_api_data, name='get_api_data'), ] ```
- This will create a new endpoint in your Django application at
http://your_domain/api-data/
which will return data from the external API when accessed.