【发布时间】:2021-02-25 16:32:25
【问题描述】:
我不熟悉 Django 和 API,我为此苦苦挣扎了好几天。 这是我的 views.py 文件:
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)
这是 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>
但是当我尝试做例如:
{{ t["height"] }}
我收到一个错误:(TemplateSyntaxError at / 无法解析余数:'["height"]' from 't["height"]')
如果这是一个愚蠢的问题,请帮助我并原谅我,我仍然是这个框架的初学者
【问题讨论】:
-
试试
{{ t.height }},属性访问、键查找和索引查找都在Django模板中使用.进行
标签: python html django dictionary