2

I Have a Django app which use api written i ASP.Net. I can call api from template (html page). Is there any way to call api from views.py? I have tried this.

from django.shortcuts import render
from django.contrib.auth.decorators import login_required

# Create your views here.

def categorydashboard(request):
     r = request.get('xxx.xxx.xx.xxx:xxxx/Category/getSubCategoryNamev2', d=request.GET)
     return render (request,'categoryDashboard.html',{})

API sample data (it is a GET request)

[
    {
        "category_id": 2,
        "category_name": "Hyper Mechanical",
        "Image_Path": null,
        "subcategory": [
            {
                "category_id": 0,
                "category_name": null,
                "product_subcategory_id": 37,
                "product_subcategory_name": "Lift",
                "schema_id": null,
                "Image_path": ""
            }
          ]
       }
]  

Server runs well but when i call 'categorydashboard' view its throws following error AttributeError: 'WSGIRequest' object has no attribute 'get' I am new in Django so i am sorry if i have mistaken

Tanveer Hasan
  • 334
  • 1
  • 5
  • 15
  • 1
    Check https://stackoverflow.com/questions/11663945/calling-a-rest-api-from-django-view , https://stackoverflow.com/questions/30259452/proper-way-to-consume-data-from-restful-api-in-django – Mate Sep 25 '19 at 04:19

1 Answers1

1

Its better to use an external library like requests. For example:

import requests
def categorydashboard(request):
     r = requests.get('xxx.xxx.xx.xxx:xxxx/Category/getSubCategoryNamev2', params=request.GET)
     return render (request,'categoryDashboard.html', context={'subcategory':r.json()})
ruddra
  • 50,746
  • 7
  • 78
  • 101