【问题标题】:I want to get the '' Alice " value from the parsed JSON in django template我想从 django 模板中解析的 JSON 中获取“Alice”值
【发布时间】:2020-07-02 05:02:54
【问题描述】:

我想从这个传递的 JSON 中将 Alice 打印为从视图到 HTML 文件的字典。

这是我的视图文件

result = json.loads(JSON_DATA.content)

return render(request, 'home.html', {'result' : result})

This is the JSON Image

{

    "firstName" : "Jane",
    "lastName" : "Dow",
    "hobbiles" : ["running", "sky diving", "singing"],
    "age" :  35,
    "children" : [
        {
            "firstName" : "Alice",
            "age" : 6,
        }, 
        {
            "firstName" : "Bob",
            "age" : 8,
        }
    ]
}

这是我在 HTML 模板中尝试过的

{{ result.children[0].firstName }}

这会引发错误 * TemplateSyntaxError* 无法从“result.children[0].firstName”解析余数“[0].firstName”

如何访问 HTML 模板中的值?

【问题讨论】:

    标签: django python-3.x django-views django-templates


    【解决方案1】:

    而不是result.children[0].firstName


    请使用此语法result.children.0.firstName

    【讨论】:

      猜你喜欢
      • 2011-03-21
      • 2019-04-23
      • 1970-01-01
      • 2018-12-08
      • 2011-04-11
      • 2022-11-21
      • 2014-10-15
      • 2018-09-23
      相关资源
      最近更新 更多