【问题标题】:How to iterate over a list in rml file of ReportLab如何遍历 ReportLab 的 rml 文件中的列表
【发布时间】:2013-07-26 14:34:47
【问题描述】:

我在 python 中创建了字典,例如

dictionary_list = [{'1':1,'2': 2,'3': 3},{'1': 1,'2':2,'3':3,'4':4},{'1':1,'2':2}]

现在我想使用 python 语法迭代这个字典。我试过这个:

{% for (key_o, val_o) in dictionary_list.items %}

但它不起作用。然后我尝试了这个,因为以前的语法没有帮助。

{% for dictionary in dictionary_list %}
     {% for (key_o, val_o) in dictionary.items %}
        {{ val_o }}
     {% endfor %}
{% endfor %}

但它仍然没有打印val_o's 值。我很沮丧,因为我无法在Report lab 的报告标记语言(RML 文件)中迭代字典列表。请指导我,谢谢。

【问题讨论】:

    标签: python django python-2.7 django-templates reportlab


    【解决方案1】:

    这是一个集合列表,而不是字典:

    >>> [ type(x) for x in [{1,2,3},{1,2,3,4},{1,2}]]
    [<type 'set'>, <type 'set'>, <type 'set'>]
    

    试试这样的:

    set_list = [{1,2,3},{1,2,3,4},{1,2}]
    {% for item in set_list %}
         {% for x in item %}
            {{ x }}
         {% endfor %}
    {% endfor %}
    

    更新:

    由于 django 标签中不允许使用括号,所以你不应该使用它们。这应该可以正常工作:

    {% for key_o, val_o in dictionary.items %}
    

    如果您只想要字典中的值而不是键,那么只需使用 dict.values:

    {% for val_o in dictionary.values %}
    

    【讨论】:

    • 抱歉问题描述有误,但您对我之前的描述是正确的,所以请您更新我的更新问题的答案,谢谢
    • @BrahamShakti 更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多