Django:如何使 json 数据在 Django 中可读?

Django: How to make json data readable in django?

我正在尝试从 hackernews api 检索最新消息,一切似乎都工作正常,当我打印状态代码时,我得到状态 Code:200。现在我正在获取一些数据,但它们不可读 这是显示的方式

b'[31349988,31344981,31348529,31344863,31341698,31348097,31347740,31348772,31347286,31348463,31345478,31348316,31345749,31347983,3'

这是我用来从 api https://hackernews.api-docs.io/

检索数据的代码
def index(request):
    response = requests.get("https://hacker-news.firebaseio.com/v0/topstories.json")
    return render(request, "index.html", {'response': response})

这是他们在模板中的样子,而不是显示标题

index.html

{% story in response %}
   {{ story.title }}
{% endfor %}

你的requests.get()returns一个对象。因此,为了使其可读,只需传递 .json() 方法,如下所示:

def index(request):
    response = requests.get("https://hacker-news.firebaseio.com/v0/topstories.json").json()
    return render(request, "index.html", {'response': response})

API 给出一个数组整数作为响应。所以我假设 API 操作错误。所以请通知 API 创建者并从他们那里获得建议。

你的 requests.get() returns 和 object。因此,为了使其可读,只需传递 .json() 方法,如下所示:

def index(request):
    response = requests.get("https://hacker-news.firebaseio.com/v0/topstories.json").json()
    return render(request, "index.html", {'response': response})

从项目中删除标题

{% story in response %}
   {{ story }}
{% endfor %}