【发布时间】:2019-01-07 00:47:01
【问题描述】:
我是 json 新手,不熟悉它。
在我的 Django 视图文件中,我只想传递某些 json 值:学校、地址、每个学生的姓名(Andy、Tom、Jack)和 id(Andy.id、Tom.id、Jack.id) 从 Django 视图到 html。如何实现上述情况?
我的json内容是:
{
"school": "xxx",
"address": "xxx",
"number": "xxx,
"students": {
"Andy": {
"id": "xxx",
"add": "xxx"
},
"Tom": {
"id": "xxx",
"add": "xxx"
},
"Jack": {
"id": "xxx",
"add": "xxx"
},
},
}
在我的 view.py 中,我有这些代码。 jresult 是 json 字符串,我使用 json.dumps() 来生成 JSON,但它会将所有 json 数据填充到 html 中。 我怎样才能实现上述情况?任何帮助将不胜感激。
def model_form_upload(request):
if request.method == 'POST':
form = FileForm(request.POST, request.FILES)
if form.is_valid():
file = request.FILES['file'].read()
jresult = execution(file)
json_string = json.dumps(jresult, indent=4)
return render(request,
'virus/home.html',
{'json_string': json_string})
else:
form = FileForm()
return render(request, 'virus/upload.html', {
'form': form
})
我的 home.html 模板:
<body>
<pre>{{ json_string|safe }}</pre>
</body>
【问题讨论】:
-
您遇到了什么问题?
-
@SachinKukreja 到目前为止,我使用 json.dumps 并将所有 json 内容呈现到 html。那不是我想要的。我只想要某个 json 值。
标签: python json django django-templates