i’m ne to Django and APIs and i’m struggling with this for days. Here’s my views.py file :
JavaScript
x
28
28
1
import requests
2
import os
3
from pprint import pprint
4
from django.views.generic import TemplateView
5
import json
6
from django.http import JsonResponse, HttpResponse
7
from django.shortcuts import render
8
9
def index(request):
10
11
12
query_url = "https://api.blockcypher.com/v1/btc/main"
13
params = {
14
"state": "open",
15
}
16
headers = {
17
}
18
result = requests.get(query_url, headers=headers, params=params)
19
json_data = result.json()
20
data = json.dumps(json_data, sort_keys=True, indent=4)
21
t = json.loads(data)
22
print(type(data))
23
print(type(t))
24
context = {
25
"t": t,
26
}
27
return render(request, "navbar/navbar.html", context)
28
And here’s the index.html :
JavaScript
1
17
17
1
{% load static %}
2
<!DOCTYPE html>
3
<html>
4
<head>
5
<title>Blockchain Explorer</title>
6
<meta charset="utf-8">
7
8
</head>
9
10
<body >
11
12
<div class="API-response">
13
{{ t }}
14
</div>
15
</body>
16
</html>
17
But when i try to do for example :
JavaScript
1
2
1
{{ t["height"] }}
2
i get an error : (TemplateSyntaxError at / Could not parse the remainder: ‘[“height”]’ from ‘t[“height”]’)
Please help me and excuse me if it is a dumb question, i’m still a beginner in this framework
Advertisement
Answer
You can access the attributes using .
in templates. Have a look at the accessing method calls documentation. You can try accessing using t.height
.