如何遍历Jinja属性:错误没有属性

时间:2018-11-02 16:14:24

标签: python html jinja2

我有一个列表“ mylist”,我想使用jinja模板呈现。我可以使用python解析字典,但是我不确定如何使用jinja2模板解析。

mylist = [
{'Author': 'jokes@tdm.com',
 'Entry Metadata': [{'u':{'_key': '16764161', '_id': 'DataPathMatch/16764161', '_from': 'DataPath/10218086',
           '_to': 'DataPath/10218123', '_rev': '_Xm4IycK--_', 'timestamp': 1539989754.3312109,
           'author': 'jokes@tdm.com', 'validated': False, 'weight': 0, 'annotation': 'This is not real.',
           'needs_human': True}}]},
{'Author': 'remcampb@cisco.com',
 'Entry Metadata': [
     {'u':
          {'_key': '16764719', '_id': 'DataPathMatch/16764719', '_from': 'DataPath/1088890',
           '_to': 'DataPath/153639', '_rev': '_Xm4I1y6--_', 'timestamp': 1539989757.7653222,
           'author': 'remcampb@cisco.com', 'validated': False, 'weight': 0, 'annotation': 'delete me',
           'needs_human': True}}]},
{'Author': 'scadora@cisco.com',
 'Entry Metadata': [
     {'u': {'_key': '16764350', '_id': 'DataPathMatch/16764350', '_from': 'DataPath/153543',
            '_to': 'DataPath/10920070', '_rev': '_Xm4Iz2q--_', 'timestamp': 1539989755.7750864,
            'author': 'scadora@cisco.com', 'validated': False, 'weight': 0, 'annotation': '',
            'needs_human': False}},
     {'u': {'_key': '16765244', '_id': 'DataPathMatch/16765244', '_from': 'DataPath/153519',
            '_to': 'DataPath/10919881', '_rev': '_Xm4I49C--_', 'timestamp': 1539989761.0025997,
            'author': 'scadora@cisco.com', 'validated': False, 'weight': 0, 'annotation': '',
            'needs_human': False}},
     {'u': {'_key': '16764203', '_id': 'DataPathMatch/16764203', '_from': 'DataPath/153823',
            '_to': 'DataPath/10018988', '_rev': '_Xm4Iytu--_', 'timestamp': 1539989754.6117036,
            'author': 'scadora@cisco.com', 'validated': False, 'weight': 0, 'annotation': '',
            'needs_human': False}},
     {'u': {'_key': '16764464', '_id': 'DataPathMatch/16764464', '_from': 'DataPath/153591',
            '_to': 'DataPath/10920028', '_rev': '_Xm4I0eK--_', 'timestamp': 1539989756.4087427,
            'author': 'scadora@cisco.com', 'validated': False, 'weight': 0, 'annotation': '',
            'needs_human': False}},
     {'u': {'_key': '16764569', '_id': 'DataPathMatch/16764569', '_from': 'DataPath/153511',
            '_to': 'DataPath/10770679', '_rev': '_Xm4I05O--_', 'timestamp': 1539989756.8448594,
            'author': 'scadora@cisco.com', 'validated': False, 'weight': 0,
            'annotation': 'last-state-transition-time is the elapsed time since last state change while ifLastChange is the sysUpTime value of the last state change',
            'needs_human': True}},
     {'u': {'_key': '16764322', '_id': 'DataPathMatch/16764322', '_from': 'DataPath/153703',
            '_to': 'DataPath/10919965', '_rev': '_Xm4Izti--_', 'timestamp': 1539989755.6299121,
            'author': 'scadora@cisco.com', 'validated': False, 'weight': 0, 'annotation': '',
            'needs_human': False}},
     {'u': {'_key': '16764747', '_id': 'DataPathMatch/16764747', '_from': 'DataPath/153679',
            '_to': 'DataPath/10919881', '_rev': '_Xm4I19a--_', 'timestamp': 1539989757.9349852,
            'author': 'scadora@cisco.com', 'validated': False, 'weight': 0, 'annotation': '', 'needs_human': False}}
 ]}]

这是我通过使用python提取数据进行解析

for dp_match in mylist:
  print(dp_match['Author'])
  for entry in dp_match['Entry Metadata']:
    print(entry['u']['_id'])

我在jinja2中尝试了以下操作,并得到一个错误:jinja2.exceptions.UndefinedError:'dict object'没有属性'u'

{% for dp_match in collaborators %}
  {{ dp_match['Author'] }}
  {% for entry in dp_match['Entry Metadata'] %}
    {{ entry['u']['_key'] }}
  {% endfor %}  
{% endfor %}

0 个答案:

没有答案