如何使用PHP-apis将数据获取到Django页面中?

时间:2019-05-27 11:36:03

标签: django django-rest-framework

我有一个PHP-api,可为我提供以下数据:

{
    "ErrorCode": 0,
    "SubErrorCode": 0,
    "ErrorMessage": "",
    "Data": [
                {
                    "id": 2,
                    "news_title": "Recent Update",
                    "news_description": "last month articles",
                    "image_id": 2,
                    "news_date": "2018-04-01",
                    "news_content": "the big content",
                    "slug": "recent-update1",
                    "company_id": 1,
                    "user_id": 1
                }
            ]
}

我想在Django中创建一个视图,该视图接受来自API的数据并将其显示在页面上。

Views.py:

from django.shortcuts import render
import requests

def blog_last_month(request):
headers = {
        "Authorization": "Bearer " + "token"
    }
    response=requests.get("/url",headers=headers,verify=False)
    data=response.json()
    return render(request,"blogHome.html",{"title":data['title']})

如何访问嵌套的Json对象元素?

0 个答案:

没有答案