【发布时间】:2019-07-17 06:37:30
【问题描述】:
我有一个包含变量的 json 字符串。
"country": {
"name":"England",
"city":"London",
"description":"This country has a land mass of {{ info.1 }}km² and population is big as <span class=\"colorF88017\">{{ info.2 }} millions</span>.",
"info":[
null,
[130395],
[5479]
]
}
如您所见,这些变量链接到 json 文件中的列表。但是当我在模板 html 中这样做时:{{ country.description }} 它没有显示info.1 or info.2 包含的内容。它只是将所有内容显示为文本。
如何在字符串中显示变量的值?
from django.template.loader import render_to_string
def country_info(request):
context = {}
show = request.GET.get("show")
if show:
context["country"] = get_country_json()
return render(request, 'country_info_index.html', context)
谢谢
【问题讨论】:
-
这个 json 是在哪里创建的?它在视图文件中吗?
-
没有从网站 api 获取 json
-
如何在 api 中填充这个?在 api 中,您需要字符串联系,但您没有这样做。
{{ country.description }}中的值将始终是 django 模板的纯文本 -
对不起,我的意思是来自在线cdn中的json文件。我从后端读取 json 并将其呈现在前端。当然,它会显示描述,但不会填充
info.1 or info.2