如何在使用jinja2时向字典对象添加新条目?

时间:2016-04-27 10:05:51

标签: python dictionary jinja2

在使用jinja2模板时,我无法追加向词典对象添加新条目。

例如,我在这里使用jinja2模板,我创建了一个数据变量,这是一个字典。检查一些 if 条件后我 WANT 将位置属性附加到数据对象,例如。

{%- set data = {
                  'name' : node.Name,
                  'id' : node.id,
               }
-%}

{% if node.location !="" %}
    data.append({'location': node.location}) 
{% endif %}

但是我找不到实现这个目的的方法并得到UndefinedError:

jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'append'

是否有人遇到此问题或者可以提供解决此问题的参考资料?

我在网上搜索但找不到解决方案,即如何在Jinja中为dict对象添加条目。

我提到了以下和其他网络资源:

  1. http://cewing.github.io/training.codefellows/assignments/day22/jinja2_walkthrough.html
  2. In Jinja2 whats the easiest way to set all the keys to be the values of a dictionary?
  3. https://github.com/saltstack/salt/issues/27494

4 个答案:

答案 0 :(得分:12)

如果没有jinja2.ext.do扩展名,您可以执行此操作:

{% set x=my_dict.__setitem__("key", "value") %}

忽略x变量并使用现在更新的字典。

UPD:此外,这适用于len()__len__()),str()__str__()),repr()__repr__())和许多类似的事情。

答案 1 :(得分:7)

关键要点:

  1. 词典不支持append()
  2. 您可以使用{% do ... %}标记将新项目添加到数据字典中,如下所示:

    {% do data.update({'location': node.location}) %}
    
  3. 但是,要使“do”标记正常工作,您需要将jinja2.ext.do扩展名添加到您的jinja环境中。

答案 2 :(得分:6)

字典没有append方法。您可以添加这样的键值对:

{% do data['location']=node.location %} 

{% do data.update({'location': node.location}) %}

答案 3 :(得分:1)

没有$string = str_split((string)$value['pattern']); $newnumber[$key] = array(); $c = 0; $append = 0; foreach($string as $number) { if($number == "[") { $append = 1; $c2 = 0; continue; } elseif ($number == "]") { $append = 0; unset($c2); continue; } elseif ($number == ".") { continue; } elseif ($number == "X") { $newnumber[$key][$c]['0'] = 0; $newnumber[$key][$c]['1'] = 1; $newnumber[$key][$c]['2'] = 2; $newnumber[$key][$c]['3'] = 3; $newnumber[$key][$c]['4'] = 4; $newnumber[$key][$c]['5'] = 5; $newnumber[$key][$c]['6'] = 6; $newnumber[$key][$c]['7'] = 7; $newnumber[$key][$c]['8'] = 8; $newnumber[$key][$c]['9'] = 9; } else { if($append == 1) { if($number == "-") { $dash = 1; continue; } elseif ($dash == 1) { $s = $newnumber[$key][$c][$c2-1]; for ($s = $s+1; $s <= $number; $s++) { $newnumber[$key][$c][$c2] = $s; $c2++; } unset($s); unset($dash); } else { $newnumber[$key][$c][$c2] = $number; $c2++; continue; } } else { $newnumber[$key][$c][] = $number; } } $c++; } /*echo "<pre>"; print_r($newnumber[$key]); echo "</pre>";*/ $numbers = array(); //Process first digit $d1 = 0; $n1arr = array(); foreach ($newnumber[$key][0] as $digit1) { $n1arr[$d1] .= $digit1; $d1++; } $d2 = 0; $n2arr = array(); foreach ($newnumber[$key][1] as $digit2) { foreach ($n1arr as $n1n) { $n2arr[$d2] = $n1n . $digit2; $d2++; } } $d3 = 0; $n3arr = array(); foreach ($newnumber[$key][2] as $digit3) { foreach ($n2arr as $n2n) { $n3arr[$d3] = $n2n . $digit3; $d3++; } } $d4 = 0; $n4arr = array(); foreach ($newnumber[$key][3] as $digit4) { foreach ($n3arr as $n3n) { $n4arr[$d4] = $n3n . $digit4; $d4++; } } $d5 = 0; $n5arr = array(); foreach ($newnumber[$key][4] as $digit5) { foreach ($n4arr as $n4n) { $n5arr[$d5] = $n4n . $digit5; $d5++; } } $d6 = 0; $n6arr = array(); foreach ($newnumber[$key][5] as $digit6) { foreach ($n5arr as $n5n) { $n6arr[$d6] = $n5n . $digit6; $d6++; } } $d7 = 0; $n7arr = array(); foreach ($newnumber[$key][6] as $digit7) { foreach ($n6arr as $n6n) { $n7arr[$d7] = $n6n . $digit7; $d7++; } } $d8 = 0; $n8arr = array(); foreach ($newnumber[$key][7] as $digit8) { foreach ($n7arr as $n7n) { $n8arr[$d8] = $n7n . $digit8; $d8++; } } $d9 = 0; $n9arr = array(); foreach ($newnumber[$key][8] as $digit9) { foreach ($n8arr as $n8n) { $n9arr[$d9] = $n8n . $digit9; $d9++; } } $d10 = 0; $n10arr = array(); foreach ($newnumber[$key][9] as $digit10) { foreach ($n9arr as $n9n) { $n10arr[$d10]['pattern'] = $n9n . $digit10; $n10arr[$d10]['description'] = $extended[$key]['description']; $n10arr[$d10]['translation'] = $extended[$key]['translation']; $n10arr[$d10]['prefix'] = $extended[$key]['prefix']; $d10++; } } 扩展名:

do

可以在基于Python 3的Jinja2基础上工作,其中{%- set _ = dict.update({c.name: c}) -%} 解决方案为我提供了

__setitem__