python - 在 Django 模板上显示来自 API 的数据

标签 python django rest api django-templates

我正在尝试将 API 上获取的数据显示到 Django 模板。

这是我尝试过的 home.html

<h1>Title: {{data.title}}</h1>

这是我的 views.py,它从 services.py 获取数据

class IndexData(TemplateView):
    def get(self, request):
        article_data = services.get_data()
        return render(request, 'pages/home.html', article_data)

这是services.py

def get_data(title, url, description, body, datePublished):
  url = 'https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/Search/WebSearchAPI'
    params = {"autoCorrect": "true", "pageNumber": "1", "pageSize": "10", "q": "police", "safeSearch": "true" }
    headers = {...}
    r = requests.get(url, headers=headers, params=params)
    data = r.json()
    article_data = {'data': data['value']}
    return article_data

返回的json

{   "_type":"all",
    "didUMean":"",
    "totalCount":2923,
    "relatedSearch":[
       "coronavirus",
       "new york",
       "post",
       "getty",
       "times",
       "china",
       "council",
       "shares",
       "americans",
       "pandemics",
       "prime",
       "19 crisis"

 ],
    "value":[      
      {         
          "title":"COVID-19 And The Failure Of Populism",
          "url":"https://www.forbes.com/sites/riskmap/2020/04/02/covid-19-and-the-failure-of-populism/",
          "description":"Populist presidents, reliant on their need to constantly convey positive messaging that bolsters their support, have struggled to take the decisive, forward-looking action that the coronavirus crisis demands.",
          "body":").\nHow will the pandemic play out in these countries?\nSteering countries through the pandemic and an eventual recovery will then be the responsibility of leaders who will have lost credibility, as well as the veneer of their all is well message. The narrative that allowed people in certain countries to latch on to something they approved of in their leader (the economy is doing well, the corruption of previous regimes was intolerable, there was no alternative, etc) while disregarding all the caveats of things they disliked, may now give way to harsher judgement. Hopefully, if there is any silver lining, the public might start trusting experts and reliable sources of information again, and will begin to question their leaders more actively.\nEither way, populist leaders like Trump, AMLO, and Bolsonaro will each have demonstrated a great inability to manage any criticism, no matter who it comes from or how constructive it might be. On the contrary, they seem to double-down",
     "keywords":"",
     "language":"en",
     "isSafe":true,
     "datePublished":"2020-04-02T11:00:00",
     "provider":{
        "name":"forbes"

 },
          "image":{
             "url":"https://thumbor.forbes.com/thumbor/fit-in/1200x0/filters%3Aformat%28jpg%29/https%3A%2F%2Fspecials-images.forbesimg.com%2Fdam%2Fimageserve%2F99b82bcbe089442095046d2dbb9ecbf9%2F0x0.jpg%3Ffit%3Dscale",
            "height":800,
        "width":1200,
        "thumbnail":"https://rapidapi.contextualwebsearch.com/api/thumbnail/get?value=37416569554313599",
        "thumbnailHeight":200,
        "thumbnailWidth":300,
        "base64Encoding":null,
        "name":null,
        "title":null,
        "imageWebSearchUrl":null

}

}

我错过了什么?

最佳答案

只需在模板中执行即可

{{ article_data.value.0.title }}

关于python - 在 Django 模板上显示来自 API 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61239669/

相关文章:

Python如何将列表作为参数并编辑其值?

python - 带索引的 Django 批量模型删除

django - drf-yasg 自定义 SwaggerUIRenderer

Django:TemplateDoesNotExist at/home.html 在我的项目中

java - Hibernate- 无法延迟初始化角色集合 : beans. Language.patients,无法初始化代理 - 无 session

Java Rest Api 返回 404

Python:在元组列表中插入计数

python - 将列表部分展平至给定级别

android - 我应该使用什么来保护 REST 网络服务中的参数

python - 如何在 pyinstaller 中仅包含所需的模块?