i’m ne to Django and APIs and i’m struggling with this for days. Here’s my views.py file :
import requests import os from pprint import pprint from django.views.generic import TemplateView import json from django.http import JsonResponse, HttpResponse from django.shortcuts import render def index(request): query_url = "https://api.blockcypher.com/v1/btc/main" params = { "state": "open", } headers = { } result = requests.get(query_url, headers=headers, params=params) json_data = result.json() data = json.dumps(json_data, sort_keys=True, indent=4) t = json.loads(data) print(type(data)) print(type(t)) context = { "t": t, } return render(request, "navbar/navbar.html", context)
And here’s the index.html :
{% load static %} <!DOCTYPE html> <html> <head> <title>Blockchain Explorer</title> <meta charset="utf-8"> </head> <body > <div class="API-response"> {{ t }} </div> </body> </html>
But when i try to do for example :
{{ t["height"] }}
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
.