Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
Python Jinja2列表未定义_Python_List_Jinja2 - Fatal编程技术网

Python Jinja2列表未定义

Python Jinja2列表未定义,python,list,jinja2,Python,List,Jinja2,在Jinja2中,我想在字典中打印“first”值的长度。在Python中,这可以通过命令len(list(my_dict.values())[0])完成 当我在Jinja2中尝试此操作时,会出现错误Jinja2.exceptions.UndefinedError:“list”未定义 最低工作示例: from jinja2 import Template, DebugUndefined from shutil import copyfile def main(file_name_templa

在Jinja2中,我想在字典中打印“first”值的长度。在Python中,这可以通过命令
len(list(my_dict.values())[0])
完成

当我在Jinja2中尝试此操作时,会出现错误
Jinja2.exceptions.UndefinedError:“list”未定义

最低工作示例:

from jinja2 import Template, DebugUndefined
from shutil import copyfile


def main(file_name_template, file_name_log):

    # Copy template file 'file_name_template' to 'file_name_log' so that logging can start.
    copyfile(file_name_template, file_name_log)
    template = Template(open(file_name_log).read(), undefined=DebugUndefined)

    # Define test variable 'my_dict'.
    my_dict = {'key_0': 4641896,
               'key_1': 189478415,
               'key_2': 841653}

    # Start logging.
    template_render_dict = {'my_dict': my_dict}

    # Save log to external file and possibly open upon completion of the 'main' program.
    template_rendered = template.render(template_render_dict)  # Render the template to the filled in log report.


if __name__ == "__main__":
    main("template.html", "template_rendered.html")
template.html中使用

<!DOCTYPE html>
<html lang="en">
    <body>
        {{my_dict}}<br/>
        {{my_dict.keys()}}<br/>
        {{my_dict.values()}}<br/>
        {{len(list(my_dict.values())[0])}}<br/>
    </body>
</html>

{{my_dict}}
{{my_dict.keys()}}
{{my_dict.values()}}
{{len(list(my_dict.values())[0])}

如何解决这个问题?

您不能在jinja2模板中使用Python
list
,因为它不是Python,而是一种标记语言。但是,它提供了一种方法。而不是:

{% if len(dict.values()[0]) > 1 %}
你应该写:

{% if dict.values()[0] | length > 1 %}

# or
{% if dict.values() | first | length > 1 %}

有关更多示例,请参见文档。

您不能在jinja2模板中使用Python
list
,因为它不是Python,而是一种标记语言。但是,它提供了一种方法。而不是:

{% if len(dict.values()[0]) > 1 %}
你应该写:

{% if dict.values()[0] | length > 1 %}

# or
{% if dict.values() | first | length > 1 %}

请参阅doc了解更多示例。

你的意思是我应该使用
{{my_dict.values()| first | length}}
而不是
{{len(list(my_dict.values())[0])}
?这确实有效,因此感谢您的提示。@Adrian您可以投票或接受答案,我将不胜感激。我之所以出现此错误,是因为我的模板的一个变量名称中有破折号。您的意思是我应该使用
{{my_dict.values()| first | length}
而不是
{len list(list(my_dict.values())[0])}
?这确实有效,因此感谢您的提示。@Adrian如果您可以投票或接受答案,我将不胜感激。我出现此错误是因为我的模板在其中一个变量名中有破折号