4👍
✅
You will have to hit your endpoints in your consuming view, the easiest way to do this is with the requests
library. First install the library:
pip install requests
Then use it in your consuming view:
def consumer_view(request):
response = requests.get('http://your-url.com/your-endpoint')
# do what you need to do here
You can use response.json()
to get the JSON response form your API as a Python dictionary. If you are just using ./manage.py runserver
your URL will be:
http:localhost:8000/your-endpoint
or
http://192.168.0.1:8000/your-endpoint
This way of consuming an API is somewhat redundant if you are working completely within Django. It’s often much easier to use the ORM in these cases. However if you are making the API to be available for outside use (either publicly or via API keys) then this approach makes sense.
Source:stackexchange.com