'Django: How to make json data readable in django?
i am trying to retrieve the lastest news from hackernews api, everthing seems to be working fine and when i print the status code, i get Status Code:200. Now i am getting some data but they are not readable this is how there are displayed
b'[31349988,31344981,31348529,31344863,31341698,31348097,31347740,31348772,31347286,31348463,31345478,31348316,31345749,31347983,3'
and this is code i used to retrieve the data from the 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})
this is how they look in the template, instead of displaying the title
index.html
{% story in response %}
{{ story.title }}
{% endfor %}
Solution 1:[1]
That API gives an array integer as a response. So I supposed API manipulation error. So plz inform the API creator and get advice from them.
Solution 2:[2]
Your requests.get()
returns an object. So, to make it readable, simply pass .json()
method, like so:
def index(request):
response = requests.get("https://hacker-news.firebaseio.com/v0/topstories.json").json()
return render(request, "index.html", {'response': response})
Solution 3:[3]
Your requests.get() returns an object. So, to make it readable, simply pass .json() method, like so:
def index(request):
response = requests.get("https://hacker-news.firebaseio.com/v0/topstories.json").json()
return render(request, "index.html", {'response': response})
remove title from item
{% story in response %}
{{ story }}
{% endfor %}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dilshan Madhuranga |
Solution 2 | Naufal Hilmiaji |
Solution 3 |