我有一个使用我在ASP.Net上编写的api的Django应用。我可以从模板(html页面)调用api。有什么办法可以从views.py调用api吗?我已经尝试过了。
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示例数据(这是一个GET请求)
[
{
"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": ""
}
]
}
]
服务器运行良好,但是当我调用“ categorydashboard”视图时,其抛出以下错误 AttributeError:“ WSGIRequest”对象没有属性“ get” 我是Django的新手,如果我输错了我很抱歉
答案 0 :(得分:1)
最好使用requests
之类的外部库。例如:
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()})