Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
访问模板在Django中传递的变量_Django_Django Templates - Fatal编程技术网

访问模板在Django中传递的变量

访问模板在Django中传递的变量,django,django-templates,Django,Django Templates,我将传递到模板: testruns,即get_list_或_404; testrun 及 dict,它是这样的smth: for testrun in testruns: dict[testrun.id] = { 'passed' : bla bla, 'failed' : bla bla 2 } 实际上,testrun.id和testrun模型集合中的一些其他信息之间的映射 在模板中,我要执行以下操作: {% for testrun in te

我将传递到模板:

testruns,即get_list_或_404; testrun

dict,它是这样的smth:

for testrun in testruns:
    dict[testrun.id] = {
        'passed' : bla bla,
        'failed' : bla bla 2
    }
实际上,testrun.id和testrun模型集合中的一些其他信息之间的映射

在模板中,我要执行以下操作:

{% for testrun in testruns %}

     console.log("{{ dict.testrun.id }}");
{% endfor %}
但不输出任何内容

console.log{{testrun.id};例如,将输出特定的id 37

console.log{{dict.37};将从dict输出相应的值

那么,为什么这个输出没有任何结果呢

console.log("{{ dict.testrun.id }}");
如何从dict中获取“通过”和“失败”数据:

此外,这:

console.log("{{ dict[testrun.id] }}");
将输出此错误:

TemplateSyntaxError at /path/dashboard

Could not parse the remainder: '[testrun.id]' from 'dict[testrun.id]'

该点将被视为模板引擎进行属性查找的触发器,因此dict.testrun.id将被解析为从dict的testrun属性中尝试查找id属性。相反,如果您想显示整个dict内容,您可以通过字典进行迭代:

{% for key, value in dict.items %}
    Testcase: {{ key }}
    Passed: {{ value.passed }}
    Failed: {{ value.failed }}
{% endfor %}
或者,如果要按变量的值查找dict,则必须创建自定义模板标记,如本文所述-

为什么要将dict用作变量?令人困惑的人。。。。