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

1

1 Answer 1

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()})
Sign up to request clarification or add additional context in comments.

1 Comment

I am getting context must be a dict rather than list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.